Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这题的测试数据有问题吧!记录数组开10*10一直给我RE,开到100*100就AC了一个这么水的DFS吃了快10次RE以为递归退出条件错了呢。。。。 后面做这题的兄弟记住了,别相信题中的数组大小,数组就是往大了开! #include <iostream> #include <algorithm> #include <cstdio> #include<queue> #include<vector> using namespace std; int dirX[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int dirY[] = {-1, 1, -2, 2, -2, 2, -1, 1}; // int visit[10][10]; 这里10*10就RE改成下面的100*100就AC int visit[100][100]; 按理说9*9都足够了(0行0列没用) int size, p, q; int note1[100], note2[100]; bool dfs(int x, int y, int pos) { int i, nextX, nextY; note1[pos] = x; note2[pos] = y; if(pos == p * q) return true; visit[x][y] = 1; for(i = 0; i < 8; i++) { nextX = x + dirX[i]; nextY = y + dirY[i]; if(nextX >= 1 && nextX <= p && nextY >= 1 && nextY <= q) { if(visit[nextX][nextY] == -1 && dfs(nextX, nextY, pos + 1)) return true; } } visit[x][y] = -1; return false; } int main() { int i, j,count = 1; bool flag; cin >> size; while(size--) { cin >> q >> p; for(i = 1; i <= p; i++) { for(j = 1; j <= q; j++) { visit[i][j] =-1; } } flag =false; for(i = 1; i <= p; i++) { for(j = 1; j <= q; j++) { if(dfs(i, j, 1)) { flag = true; break; } } if(flag) break; } cout << "Scenario #" << count++ <<":" << endl; if(flag) { for(i = 1; i <= p * q; i++) cout << (char)(note1[i] - 1 + 'A') << note2[i]; cout << endl; } else cout << "impossible" << endl; cout << endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator