标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 41936 | Accepted: 14269 |
Description
Input
Output
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
Source
// // main.cpp // poj2488 // // Created by Candy on 9/27/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=50; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x; } int T,n,m,sum,vis[N][N],flag=0,cas=0; struct data{ int x,y; data(int a=0,int b=0):x(a),y(b){} }path[N]; int dx[8]={-1,1,-2,2,-2,2,-1,1},dy[8]={-2,-2,-1,-1,1,1,2,2}; void print(){ for(int i=1;i<=sum;i++){ int x=path[i].x,y=path[i].y; printf("%c%d",‘A‘-1+y,x); } } void dfs(int x,int y,int d){//printf("dfs %d %d %d\n",x,y,d); path[d]=data(x,y); if(d==sum){flag=1;return;} for(int i=0;i<8;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&!vis[nx][ny]&&!flag){ vis[nx][ny]=1; dfs(nx,ny,d+1); vis[nx][ny]=0; } } } int main(int argc, const char * argv[]) { T=read(); while(T--){ n=read();m=read(); sum=n*m; flag=0; memset(vis,0,sizeof(vis)); vis[1][1]=1; dfs(1,1,1); printf("Scenario #%d:\n",++cas); if(!flag) printf("impossible");else print(); printf("\n\n"); } return 0; }
POJ2488A Knight's Journey[DFS]
标签:
原文地址:http://www.cnblogs.com/candy99/p/5914851.html