1152. 简单的马周游问题

来源:互联网 发布:照片转卡通软件 编辑:程序博客网 时间:2024/06/06 00:39

找到后break要放到深搜后面呢……被阴了几次

#include <iostream>#include <cstring>using namespace std;bool been[5][6];int dir[8][2];int route[30];int count;bool found;void initial() {dir[0][0] = -2, dir[0][1] = -1;dir[1][0] = -2, dir[1][1] = 1;dir[2][0] = 2, dir[2][1] = -1;dir[3][0] = 2, dir[3][1] = 1;dir[4][0] = -1, dir[4][1] = -2;dir[5][0] = -1, dir[5][1] = 2;dir[6][0] = 1, dir[6][1] = -2;dir[7][0] = 1, dir[7][1] = 2;memset(been, false, sizeof(been));memset(route, 0, sizeof(route));count = 0;found = false;}int point(const int x, const int y) {return x*6+y+1;}bool valid(const int x, const int y) {return (x >= 0 && x <= 4 && y >= 0 && y <= 5);}void dfs(int x, int y) {if(found) {return;}for(int i = 0; i < 8; i++) {int newx = x + dir[i][0];int newy = y + dir[i][1];if(valid(newx, newy) && !been[newx][newy]) {route[count++] = point(newx, newy);been[newx][newy] = true;if(count == 30) {found = true;}dfs(newx, newy); if(found) {break;}    been[newx][newy] = false;    count--;}}}void print() {cout << route[0];for(int i = 1; i <= 29; i++) {cout << ' ' << route[i];}cout << endl;}int main() {int n;while(cin >> n && n != -1) {initial();route[count++] = n;int st_x = (n-1)/6, st_y = n%6-1;been[st_x][st_y] = true;dfs(st_x, st_y);print(); }return 0;} 


0 0
原创粉丝点击