标签:dspan idtransmarkspa span idtransmarkspan
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 35974 | Accepted: 12272 |
Description
Input
Output
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
Source
TUD Programming Contest 2005, Darmstadt, Germany
题意是:看 马 是否可以在不重复走相同点的情况下,将所有的点都遍历一遍,并按字典序输出 .
A B C D E F
1
2
3
4
</pre><pre name="code" class="cpp">#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #define INF 0x3f3f3f3f using namespace std; int n,m,num=0,flag=0; int hang[100]; char lie[100]; int vis[100][100]; int dx[]= {-1,1,-2,2,-2,2,-1,1}; //注意顺序,为字典序输出 int dy[]= {-2,-2,-1,-1,1,1,2,2}; int Judge(int x,int y) { if(x<=n&&y<=m&&x>0&&y>0) return 1; return 0; } int DFS(int x,int y,int ans) { if(!Judge(x,y)) return 0; if(ans==n*m) { hang[ans]=x; lie[ans]=(char)(y-1+'A'); flag=1; return ans; } if(vis[x][y]==0&&!flag) //!flag 会保证第一次遍历成功后就不遍历了. { hang[ans]=x; lie[ans]=(char)(y-1+'A'); vis[x][y]=1; for(int i=0; i<8; i++) { int fx=dx[i]+x; int fy=dy[i]+y; if(Judge(fx,fy)&&vis[fx][fy]==0) { DFS(fx,fy,ans+1); } } vis[x][y]=0; //找不到还原 } } int main() { int T; int Case=0,kk=1; while(~scanf("%d",&T)) { while(T--) { if(kk==1) kk=0; else printf("\n"); flag=0; num=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); DFS(1,1,1); Case++; printf("Scenario #%d:\n",Case); if(flag==0) { printf("impossible\n"); continue; } for(int i=1; i<=n*m; i++) printf("%c%d",lie[i],hang[i]); printf("\n"); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:dspan idtransmarkspa span idtransmarkspan
原文地址:http://blog.csdn.net/became_a_wolf/article/details/47998713