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个回答

代码其它部门没有问题

并且#include 要有

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

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

wuli_Susu
wuli_Susu int main() { int n; vector<int> 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++) { int o = Count[i]; cout << o << endl; } return 0; }我改成了这样,但还是错误,也包含了向量的头文件,。。。不知道
2 年多之前 回复

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

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

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
hany2008 cout<<Count.at(i)<<endl;试试吧
2 年多之前 回复
hany2008
hany2008 可能是vector用的有问题,在for循环中输出时,采用cout<<
2 年多之前 回复
wuli_Susu
wuli_Susu wuli_Susu int main() { int n; vector<int> 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++) { int o = Count[i]; cout << o << endl; } return 0; }我改成了这样,但还是错误,也包含了向量的头文件,。。。不知道
2 年多之前 回复

#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;
}

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐