您的位置:首页 > 其它

POJ 2488 A Knight's Journey

2014-09-07 22:15 148 查看

A Knight's Journey

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2488
64-bit integer IO format: %lld Java class name: Main

Background

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int n,m,path[100][2];
bool vis[100][100];
bool dfs(int x,int y,int cur){
if(cur >= n*m) return true;
for(int i = 0; i < 8; i++){
int tx = dir[i][0]+x;
int ty = dir[i][1]+y;
if(tx < 0 || tx >= n || ty < 0 || ty >= m || vis[tx][ty]) continue;
vis[tx][ty] = true;
path[cur][0] = tx+'A';
path[cur][1] = ty+1;
if(dfs(tx,ty,cur+1)) return true;
vis[tx][ty] = false;
}
return false;
}
int main() {
int t,k = 1;
scanf("%d",&t);
while(t--){
scanf("%d %d",&m,&n);
bool flag = false;
memset(vis,false,sizeof(vis));
path[0][0] = 'A';
path[0][1] = 1;
vis[0][0] = true;
printf("Scenario #%d:\n",k++);
if(dfs(0,0,1)){
for(int i = 0; i < n*m; i++) printf("%c%d",path[i][0],path[i][1]);
puts("");
}else puts("impossible");
puts("");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: