A - Shortest path of the king
来源:互联网 发布:windows xp是什么意思 编辑:程序博客网 时间:2024/05/17 18:43
Crawling in process...Crawling failedTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, because he has business of national importance. For example, he has to pay an official visit to squaret. As the king is not in habit of wasting his time, he wants to get from his current positions to square t in the least number of moves. Help him to do this.
In one move the king can get to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).
Input
The first line contains the chessboard coordinates of square s, the second line — of square t.
Chessboard coordinates consist of two characters, the first one is a lowercase Latin letter (froma to h), the second one is a digit from1 to 8.
Output
In the first line print n — minimum number of the king's moves. Then inn lines print the moves themselves. Each move is described with one of the 8:L, R,U, D,LU, LD,RU or RD.
L, R,U, D stand respectively for moves left, right, up and down (according to the picture), and 2-letter combinations stand for diagonal moves. If the answer is not unique, print any of them.
Sample Input
a8h1
7
RD
RD
RD
RD
RD
RD
RD
题意:类似poj2488,给出两个点的坐标,八个方向,问最短的路径。
思路:bfs记录路径。
#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;int sx,sy,ex,ey;int path[100];int dir[8][2]={1,1,1,-1,-1,-1,-1,1,0,-1,1,0,-1,0,0,1};//RU,RD,LD,LU,D,R,L,Ustruct node{ int x; int y; int px,py; int step; int d; int visit;}a[10][10];int bfs(int n){ memset(a,0,sizeof(a)); queue<node> q; node cur; a[sx][sy].x=sx; a[sx][sy].y=sy; a[sx][sy].step=0; a[sx][sy].visit=1; q.push(a[sx][sy]); int aa=1<<29; while(!q.empty()) { cur=q.front();//赋值 q.pop(); if(cur.x==ex&&cur.y==ey) { //printf("cur=%d\n",cur.step); aa=cur.step; break; } for(int i=0;i<8;i++) { int tx=cur.x+dir[i][0]; int ty=cur.y+dir[i][1]; if(tx>0&&tx<=n&&ty>0&&ty<=n&&a[tx][ty].visit==0) { a[tx][ty].visit=1; a[tx][ty].px=cur.x; a[tx][ty].py=cur.y; a[tx][ty].x=tx; a[tx][ty].y=ty; a[tx][ty].step=cur.step+1; a[tx][ty].d=i; q.push(a[tx][ty]); } } } return aa;}int main(){ char s,e; int n=8; scanf("%c%d",&s,&sy); getchar(); sx=(s-'a')+1; scanf("%c%d",&e,&ey); getchar(); ex=(e-'a')+1; int w=bfs(n); printf("%d\n",w); if(w==0) return 0; int tx=a[ex][ey].px,ty=a[ex][ey].py; int i=0; path[i++]=a[ex][ey].d; int mm,nn; while(tx!=sx||ty!=sy) { //cout<<tx<<" "<<ty<<endl; path[i++]=a[tx][ty].d; mm=a[tx][ty].px; nn=a[tx][ty].py; tx=mm; ty=nn; } for(int j=i-1;j>=0;j--) { switch(path[j]) { case 0://RU,RD,LD,LU,D,R,L,U printf("RU\n"); break; case 1: printf("RD\n"); break; case 2: printf("LD\n"); break; case 3: printf("LU\n"); break; case 4: printf("D\n"); break; case 5: printf("R\n"); break; case 6: printf("L\n"); break; case 7: printf("U\n"); break; } } return 0;}
- A - Shortest path of the king
- A. Shortest path of the king
- A. Shortest path of the king
- 3A - Shortest path of the king
- Shortest path of the king
- Shortest path of the king
- CF 3A Shortest path of the king
- Shortest path of the king(比赛2.A)
- coderforce 3A-Shortest path of the king
- codeforces 3A - Shortest path of the king
- Codeforces 3A Shortest path of the king BFS
- code forces 3A Shortest path of the king
- codeforces 3A Shortest path of the king
- 【pair_简单贪心】#3 A. Shortest path of the king
- Codeforces 3A Shortest Path of the King
- Codeforces Round #3A. Shortest path of the king
- Codeforces Beta Round #3 A. Shortest path of the king
- Codefoeces 3A. Shortest path of the king
- Erlang socket 工作原理
- UFT/QTP11.5新特性
- 【跟师兄学编程】换电脑引发的血案
- 边缘填充算法
- 霍夫曼编码
- A - Shortest path of the king
- IO / Linux系统对IO端口和IO内存的管理
- 深入了解计算机系统 练习题2.74
- 关于SQL Server系统数据库详解
- .Net中后台线程和前台线程的区别
- eclipse 创建 userlibraries 库
- 五子棋算法篇
- SQL LIKE 通配符
- 淘宝页面实现(包括级联菜单,下拉列表,框架)