hrbust

            影子模仿术    

Time Limit: 500 MS Memory Limit: 32768 K
Total Submit: 7(5 users) Total Accepted: 2(2 users) Rating: Special Judge: No
Description

鹿丸看见了一个胖子。

“胖子!”

“啊?”

胖子转身,面对面发现站着的鹿丸。奇怪之于,他突然发现自己的脚动不了了。原来他中了鹿丸的影子模仿术。

“你想干嘛?”胖子怒吼。

“我想采访你一下,来,过来”。说着,鹿丸向前走去。胖子发现自己的行动被鹿丸控制了,鹿丸向前走,他也向前走。鹿丸左转,他也左转。

“叫我过去就过去?”胖子一生气,只听见duang的一声,天崩地裂,凭空从地上出现几座大山,不能通行。而此时,胖子和鹿丸的位置也被震的发生了变化,但是影子模仿术没被解除。此时两人一人面向东方,一人面向西方。

鹿 丸不料胖子有这等能耐。不过这下可费神了,怎么走到胖子旁边 呢?鹿丸往前走,胖子也会往前走,鹿丸不能往一个障碍物前进,但如果鹿丸前进时,胖子前进的位置是障碍物,那么胖子会狠狠的撞上去,不过实际的位置并不会 变化。注意,这里说的前是相对的,两人面向何方,那么前就是何方。当然,无论何时,鹿丸只要转向,胖子也会跟着转。

但是现在地形这么复杂,鹿丸需要思考一下,要怎么走才能用最少的步数让自己和胖子走到同一个地方,或者相邻也可以。毕竟和胖子挤一个地方还是挺憋屈的。
Input
多组测试数据
每组测试数据第一行有两个数N,M (2<= N <= 20, 2 <= M <= 20) 表示场地的大小。
接下来有N行,每行有M个字符。描述场地的状态。其中'X'表示障碍,'P'表示胖子,'L'表示鹿丸。
Output
对于每组数据,输出最小的总步数。如果不能相见,则输出"Smart PangZi!"
Sample Input
3 3
PXL
...
...
3 3
PX.
...
.XL
Sample Output
3
2
Hint

多提供几组样例数据

Input:

3 3

P..

L..

...

3 3

XPX

L..

...

3 3

PXL

.X.

...

3 3

PX.

..X

L..


Output:

0

1

5

1

Source
哈尔滨理工大学第五届ACM程序设计竞赛(热身)
/**

**/
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<queue>
#define maxn 25
using namespace std;
char ch[maxn][maxn];
int vis[maxn][maxn][maxn][maxn];
int n,m;
bool prime =false;
int tx[4] = {0,0,1,-1};
int ty[4] = {-1,1,0,0};
int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0}; ///上 下 左 右
struct Node
{
    int x1;
    int y1;
    int x2;
    int y2;
    int step;
    Node()
    {
        x1 = 0;
        y1= 0;
        x2 = 0;
        y2 = 0;
        step = 0;
    }
} start;
//struct cmp
//{
//    bool operator() (const  Node a,const Node b)const
//    {
//        return ((a.x1 - a.x2) *(a.x1 - a.x2) +(a.y2 -  a.y1)*(a.y2 -  a.y1) >  (b.x1 - b.x2) *(b.x1 - b.x2) +(b.y2 -  b.y1)*(b.y2 -  b.y1));
//    }
//};
int check(int x1,int y1)
{
    if(x1 >=0 && x1 <n && y1 >=0 && y1 <m) return 1;
    return 0;
}
double solve(Node a)
{
    return (a.x1 - a.x2) *(a.x1 - a.x2) +(a.y2 -  a.y1)*(a.y2 -  a.y1);
}
queue<Node>que;
int bfs()
{
    while(!que.empty()) que.pop();
    Node tmp,now;
    vis[start.x1][start.y1] [start.x2][start.y2]= 1;
    start.step = 0;
    Node temp;
    que.push(start);
    while(!que.empty())
    {
        now = que.front();
        que.pop();
        int tt = solve(now);
        if(tt == 0 || tt == 1)
        {
            return now.step;
        }
        for(int i=0; i<4; i++)
        {
            temp.x1 = now.x1 + dx[i];
            temp.y1 = now.y1+ dy[i];
            if(temp.x1 < 0 || temp.x1 >= n || temp.y1 < 0 || temp.y1 >= m || ch[temp.x1][temp.y1] == 'X') continue;
            int u = now.x2 + tx[i];
            int v  = now.y2 + ty[i];
            if(u <0 || u>=n || v <0 || v >=m || ch[u][v] == 'X')
            {
                temp.x2 = now.x2;
                temp.y2 = now.y2;
            }
            else
            {
                temp.x2 = u;
                temp.y2 = v;
            }
            temp.step = now.step + 1;
            if(vis[temp.x1][temp.y1][temp.x2][temp.y2] == 0)
            {
                vis[temp.x1][temp.y1][temp.x2][temp.y2]  = 1;
                que.push(temp);
            }
        }
    }
    return -1;
}
int main()
{
//#ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%s",ch[i]);
            for(int j=0; j<m; j++)
            {
                if(ch[i][j] == 'P')
                {
                    start.x2 = i;
                    start.y2 = j;
                }
                else if(ch[i][j] == 'L')
                {
                    start.x1 = i;
                    start.y1 = j;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        int res = bfs();
        if(res == -1) printf("Smart PangZi!
");
        else  printf("%d
",res);
    }
    return  0;
}
原文地址:https://www.cnblogs.com/chenyang920/p/4374751.html