Shortest path of the king(比赛2.A)

1.题目:

Home	Problems	Status	Contest	[acgirl]	Logout
NEFU要崛起——第2场
3:00:00
OverviewProblemStatusRankDiscuss

A
 
B
 
C
 
D
A - Shortest path of the king
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
 
Practice
 
CodeForces 3A
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 square t. As the king is not in habit of wasting his time, he wants to get from his current position s 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 (from a to h), the second one is a digit from 1 to 8.

Output
In the first line print n — minimum number of the king's moves. Then in n 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
Input
a8
h1
Output
7
RD
RD
RD
RD
RD
RD
RD
FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2012 HUST ACM/ICPC TEAM 
Anything about the OJ, please ask in the forum, or contact author:Isun
Server Time: 


 

2.题意:已知起点,终点,可以上、下、左、右、左上、左下、右上、右下8个方向走。输出最短路径及每步走的方向

3.思路:bfs

4.一直wrong,待改正

wrong代码:

#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};
struct 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;
    while(tx!=sx&&ty!=sy)
    {
        path[i++]=a[tx][ty].d;
        tx=a[tx][ty].px;
        ty=a[tx][ty].py;
    }
    for(int j=i-1;j>=0;j--)
    {
        switch(path[j])
        {
            case 0:
            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("RU\n");
            break;
        }
    }
    return 0;
}


 

4.accept代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int move[8][2]={{0,-1},{0,1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
//             左 L    右  R 下  D  上 U    右下 RD  左下  LD  右上RU    左上LU
char mo[8][3]={'L','R','D','U','RD','LD','RU','LU'};
int ex,ey,sx,sy;
struct node{
    int x,y;
    int vis;
    int depth;
    int shx,shy;
    int m;
}pan[10][10];
int bfs()
{
    int i,j,k;
    queue<node> q;
    q.push(pan[sx][sy]);
    if(sx==ex&&sy==ey)
    return 0;
    pan[sx][sy].vis=1;
    while(!q.empty())
    {
        node p;
        int tx,ty,yx,yy;
        p=q.front();
        q.pop();

            for(i=0;i<8;i++)
            {
				node t;
				t=p;
                tx=p.x+move[i][0];
                ty=p.y+move[i][1];
                if(tx<0||tx>=8||ty<0||ty>=8)
                    continue;
                if(pan[tx][ty].vis==0)
                {
                    pan[tx][ty].shx=p.x;
					pan[tx][ty].shy=p.y;
                    pan[tx][ty].depth=p.depth+1;
                    q.push(pan[tx][ty]);
                    pan[tx][ty].vis=1;
                    pan[tx][ty].m=i;

                    if(tx==ex&&ty==ey)
                        return pan[tx][ty].depth;
                }
            }


    }
    return 0;
}
void init()
{
    int i,j;
    for(i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
        {
            pan[i][j].x=i;
            pan[i][j].y=j;
        }
    }
}
int main()
{
    int i,j,k,l,m,n;
    memset(pan,0,sizeof(pan));
    init();
    char z,v;
    scanf("%c%c",&z,&v);//先y(字母)后x
    sy=z-'a';
	sx=8-v+48;
	getchar();
    scanf("%c%c",&z,&v);
    ey=z-'a';
	ex=8-v+48;
	m=bfs();
    printf("%d\n",m);
    if(m==0)
    return 0;
    int  path[100];
    memset(path,0,sizeof(path));
    j=0;
    path[j++]=pan[ex][ey].m;
	int qx,qy;
	qx=pan[ex][ey].shx;
	qy=pan[ex][ey].shy;
    while(qx!=sx||qy!=sy)
    {
		int tx,ty;
		tx=qx;ty=qy;
		qx=pan[tx][ty].shx;
		qy=pan[tx][ty].shy;
        path[j++]=pan[tx][ty].m;
        //printf("%d\n",p->m);
    }
    for(i=j-1;i>=0;i--)
	{
		int k=path[i];
		if(k==0)
		printf("L\n");
		else if(k==1)
		printf("R\n");
		else if(k==2)
		printf("D\n");
		else if(k==3)
		printf("U\n");
		else if(k==4)
		printf("RD\n");
		else if(k==5)
		printf("LD\n");
		else if(k==6)
		printf("RU\n");
		else if(k==7)
		printf("LU\n");
	}

return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值