wuli_Susu
2018-01-22 13:41
采纳率: 100%
浏览 1.0k

ZOJ的1002题,我试了很多输入但找不到错在哪里,在OJ上是WA,拜托大神们帮我看看

题目地址
#include
using namespace std;

char* CreateMap(char map[][4],int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
return *map;
}
void PrintMap(char map[][4], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout<< map[i][j];
}
cout << endl;
}
}

bool Judge(char map[][4], int n,int i,int j) {
char m = map[i][j];
if (m == 'X' || m == 'O') return false;
if (m == '.') {
int p, q;
bool flag = true;
for (p = j-1; p >=0; p--) {
if (map[i][p] == 'O')flag = false;
if (map[i][p] == 'X')break;
}
for (p = j + 1; p < n; p++) {
if (map[i][p] == 'O')flag = false;
if (map[i][p] == 'X')break;
}
for (q = i - 1; q >= 0; q--) {
if (map[q][j] == 'O')flag = false;
if (map[q][j] == 'X')break;
}
for (q = i + 1; q < n; q++) {
if (map[q][j] == 'O')flag = false;
if (map[q][j] == 'X')break;
}
return flag;
}
}

int SearchMap(char map[][4],int n,int i,int j) {
bool flag;
int count = 0;
int p, q;
p = i;
for (q = j; q < n; q++) {
flag = Judge(map, n, p, q);
if (flag) {
map[p][q] = 'O';count++;
}
}
for (p = i + 1; p < n; p++) {
for (q = 0; q < n; q++) {
flag = Judge(map, n, p, q);
if (flag) {
map[p][q] = 'O'; count++;
}
}
}
return count;
}
void ReSet(char map[][4], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == 'O')map[i][j] = '.';
}
}
}

int MaxSearch(char map[][4], int n) {
int count = 0;
int j,h,k;
int i = 0;
for (j = 0; j < n; j++) {
int count2 = SearchMap(map, n, i, j);
ReSet(map, n);
if (count2 > count) {
count = count2; h = i; k = j;
}
}
return count;
}

int main() {
int n;
while (cin>>n&&n!=0) {
char map[4][4];
CreateMap(map, n);
int count = MaxSearch(map, n);
cout << count << endl;
}
return 0;
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • hany2008 2018-01-22 16:04
    已采纳

    代码其它部门没有问题

    打赏 评论
  • hany2008 2018-01-22 14:13

    主函数main中把cin>>n 作为独立语句来存在,试试

    打赏 评论
  • hany2008 2018-01-22 14:14

    并且#include 要有

    打赏 评论
  • threenewbee 2018-01-22 15:08

    图片说明
    试验了下你的程序,是输入1个输出1个,但是OJ要求输入完,直到0,再输出。

    打赏 评论
  • threenewbee 2018-01-22 15:11

    你可以先用一个数组把输入记录下来,分别计算,再将结果依次输出,应该就能过了。

    打赏 评论
  • hany2008 2018-01-22 16:03

    int main() {
    int n;
    cin>>n;
    while (n!=0) {
    char map[4][4];
    CreateMap(map, n);
    int count = MaxSearch(map, n);
    cout << count << endl;
    cin>>n;
    }
    return 0;
    }
    以上代码是把main函数的输入改写了一下,可以实现连续输入,且验证与题目要求输入输出一致,不知能否解决你的问题。

    打赏 评论
  • hany2008 2018-01-22 16:09

    如果要连续输出结果,可以在main函数中,把每次得到的结果存入数组中,在main函数中的while循环外,再利用循环输出即可解决。

    打赏 评论
  • hany2008 2018-01-23 06:53

    #include
    #include
    using namespace std;
    char* CreateMap(char map[][4],int n) {
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
    cin >> map[i][j];
    }
    }
    return *map;
    }
    void PrintMap(char map[][4], int n) {
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
    cout<< map[i][j];
    }
    cout << endl;
    }
    }
    bool Judge(char map[][4], int n,int i,int j) {
    char m = map[i][j];
    if (m == 'X' || m == 'O') return false;
    if (m == '.') {
    int p, q;
    bool flag = true;
    for (p = j-1; p >=0; p--) {
    if (map[i][p] == 'O')flag = false;
    if (map[i][p] == 'X')break;
    }
    for (p = j + 1; p < n; p++) {
    if (map[i][p] == 'O')flag = false;
    if (map[i][p] == 'X')break;
    }
    for (q = i - 1; q >= 0; q--) {
    if (map[q][j] == 'O')flag = false;
    if (map[q][j] == 'X')break;
    }
    for (q = i + 1; q < n; q++) {
    if (map[q][j] == 'O')flag = false;
    if (map[q][j] == 'X')break;
    }
    return flag;
    }
    }
    int SearchMap(char map[][4],int n,int i,int j) {
    bool flag;
    int count = 0;
    int p, q;
    p = i;
    for (q = j; q < n; q++) {
    flag = Judge(map, n, p, q);
    if (flag) {
    map[p][q] = 'O';count++;
    }
    }
    for (p = i + 1; p < n; p++) {
    for (q = 0; q < n; q++) {
    flag = Judge(map, n, p, q);
    if (flag) {
    map[p][q] = 'O'; count++;
    }
    }
    }
    return count;
    }
    void ReSet(char map[][4], int n) {
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
    if (map[i][j] == 'O')map[i][j] = '.';
    }
    }
    }
    int MaxSearch(char map[][4], int n) {
    int count = 0;
    int j,h,k;
    int i = 0;
    for (j = 0; j < n; j++) {
    int count2 = SearchMap(map, n, i, j);
    ReSet(map, n);
    if (count2 > count) {
    count = count2; h = i; k = j;
    }
    }
    return count;
    }
    int main()
    { int n;
    vector Count;
    cin >> n;
    while (n!=0)
    { char map[4][4];
    CreateMap(map, n);
    int count = MaxSearch(map, n);
    Count.push_back(count);
    cin >> n;
    }
    int m = Count.size();
    for (int i = 0; i < m; i++)
    {
    cout <<Count.at(i) << endl;
    }
    return 0;
    }

    打赏 评论
  • hany2008 2018-01-23 06:56

    刚才提交的代码可以运行,且结果正确。

    打赏 评论

相关推荐 更多相似问题