hrbust - 2239

            影子模仿术    

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程序设计竞赛(热身)
/**
          题意:求L到R的最短距离
          做法:bfs + 优先队列;
          代码是WA 求解???
**/
#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);
}
priority_queue<Node,vector<Node>,cmp>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.top();
        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(check(temp.x1,temp.y1) && ch[temp.x1][temp.y1] == ‘.‘)
            {
                int u = now.x2 + tx[i];
                int v  = now.y2 + ty[i];
                if(check(u,v) && ch[u][v] == ‘.‘)
                {
                    temp.x2 = u;
                    temp.y2 = v;
                }
                else
                {
                    temp.x2 = now.x2;
                    temp.y2 = now.y2;
                }
                if(vis[temp.x1][temp.y1][temp.x2][temp.y2] == 0)
                {
                    temp.step = now.step + 1;
                    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!\n");
        else  printf("%d\n",res);
    }
    return  0;
}
时间: 2024-08-26 07:03:59

hrbust - 2239的相关文章

[POJ] 2239 Selecting Courses(二分图最大匹配)

题目地址:http://poj.org/problem?id=2239 Li Ming大学选课,每天12节课,每周7天,每种同样的课可能有多节分布在不同天的不同节.问Li Ming最多可以选多少节课.把n种课划分为X集合,把一周的84节课划分为Y集合, 从Xi向Yi连边,那么就转化成了求二分图的最大匹配数,然后匈牙利算法就可以了. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #includ

HRBUST 1214 方格取数

方格取数 Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged on HRBUST. Original ID: 121464-bit integer IO format: %lld      Java class name: Main 设有N*N的方格图(N<=10),我们将其中的某些方格中填入正整数,而其他的方格中则放人数字0.如下图所示(见样例 ,黄色和蓝色分别为两次走的路线,其中绿色的格子为黄色和蓝色共同走

POJ 2239 Selecting Courses(二分匹配)

题目链接:http://poj.org/problem?id=2239 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning

HRBUST 1161 树状数组区间更新求和

Leyni Time Limit: 3000 MS Memory Limit: 65536 K Total Submit: 267(64 users) Total Accepted: 82(57 users) Rating: Special Judge: No Description Leyni被人掳走,身在水深火热之中... 小奈叶为了拯救Leyni,独自一人前往森林深处从静竹手中夺回昏迷中的Leyni. 历经千辛万苦,小奈叶救出了Leyni,但是静竹为此极为恼怒,决定对他们发起最强烈的进攻.

POJ 2239 Selecting Courses【最大匹配】

大意: 有一些课程,每个课程的上课时间在每周可能会有多节,你可以从中任选一个时间去听课, 每周七天一天12节课  告诉你每节课的上课时间问在不冲突的情况下最多上几节课? 分析: 左集合课程 右集合时间 边为该节课对应的上课时间 求最大匹配就行了 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std

poj——2239 Selecting Courses

poj——2239   Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10656   Accepted: 4814 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the c

二分图最大匹配题目汇总 POJ 1274、2239、3020、3715

POJ 1274 #include <iostream> #include <string.h> #include <vector> #include <cstdio> using namespace std; vector<int> aa[205]; int flag[205],visted[205]; bool dfs(int x){ for(int i=0;i<aa[x].size();++i){ if(!visted[aa[x][i

POJ 2239 二分图匹配/Hungary

Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8667 Accepted: 3863 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming i

hrbust 1840 (树状数组第k大) 删点使用

小橙子 Time Limit: 2000 MS Memory Limit: 32768 K Total Submit: 2(2 users) Total Accepted: 1(1 users) Rating:  Special Judge: No Description 玻璃小屋里曾经住着一个小橙子.小橙子小小的,性格很直.直性子的小橙子傻乎乎的,还很爱看火影.海贼王什么的.他收藏了很多动画片的光碟,光碟太多了,他打算整理一下. 他口中念念有词:“这个,插入第三个光碟前面,这个,插入第五个光碟