离奇的错误,样例和手敲了几组都过了,迷惑。
#include <iostream>
using namespace std;
const int dx[5] = {0, 1, 0, -1, 0},dy[5] = {0, 0, 1, 0, -1};
int n, m, k;
int a[501][501]= {};
int book[501][501]= {};
int xx[300],yy[300];
int x=1,y=1,f = 1;
void getway(int x1,int y1) {
switch(f) {
case 1:
if(!book[x1+1][y1])f = 2;
break;
case 2:
if(!book[x1][y1+1])f = 3;
break;
case 3:
if(!book[x1-1][y1])f = 4;
break;
case 4:
if(!book[x1][y1-1])f = 1;
break;
default:;
}
}
void print(int t) {
cout << t <<" ";
for(int i=1; i <= t; i ++) {
cout << xx[i] << " " << yy[i] << " ";
}
cout << endl;
}
void check(int t,int l) {
xx[t] = x,yy[t] = y;
getway(x,y);
book[x][y] = 0;
x += dx[f],y += dy[f];
if(l != k) {
if(t == 2) {
print(2);
return;
}
} else if(t == n*m - (k-1)*2) {
print(t);
return;
}
check(t + 1, l);
}
int main() {
cin >> n >> m >> k;
for(int i=1 ; i <= n; i ++) {
for(int j= 1; j <= m; j ++) {
book[i][j] = 1;
}
}
for(int l = 1; l <= k; l ++) {
check(1,l);
}
return 0;
}