HDU 1242, hdu1242

Source: Internet
Author: User

HDU 1242, hdu1242

Link: Rescue


The progress has dropped too much. I always complain that the progress is slower than others... Why don't you try to change it ....


I thought it was a water question. I wanted to try it out. Later I found that it was not a simple search question. BFS was definitely doing something wrong... Later, I found that there were pitfalls in the question.

It is the shortest distance from r to a, "." is equivalent to 1, and "x" is equivalent to 2.


HDU search courseware said that this question is similar to HDU1010. At the beginning, I didn't feel like pruning, so I switched to two-way BFS 0 ms a Y!

After checking on the Internet, the cool actually used BFS + as the priority queue... Epiphany


So the search tree in this question can be understood as root as a, connecting several branches, and the leaf of the branches is r. The answer is naturally the answer if the depth of the branches is the smallest; use the priority queue to control and filter large branches in depth for pruning.

After a few clicks, BFS + priority queue 15 ms a Y, I believe that if you optimize the ans storage, the smaller ans will be saved first, and 0 ms is very easy.


Send special data:

3 3

..

X #.

. R.

Print 4



The BFS + priority queue code is as follows:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>const int N = 210;using namespace std;struct node{    int x, y,ans;   friend bool operator <(const node &a,const node &b)    {        return a.ans>b.ans;    }};int n, m;char ma[N][N];bool vis[N][N];int sx, sy;int mv[4][2] = {{-1,0},{0,1},{0,-1},{1,0}};void BFS(int sx,int sy){    priority_queue<node> q;    node f, t;    f.x = sx, f.y = sy, f.ans = 0;    vis[sx][sy] = true;    q.push(f);    while(!q.empty())    {        t = q.top();         if(ma[t.x][t.y]=='r')            {                cout<<t.ans<<endl;                return ;            }        q.pop();        for(int i=0; i<4; i++)        {            f.x = t.x +mv[i][0];            f.y = t.y +mv[i][1];            if(!vis[f.x][f.y]&&0<=f.x&&f.x<n&&0<=f.y&&f.y<m&&ma[f.x][f.y]!='#')            {                if(ma[f.x][f.y]=='x')               {                 f.ans = t.ans+2;                 q.push(f);               }               else              {                f.ans = t.ans+1;                q.push(f);              }              vis[f.x][f.y] = true;            }        }    }     cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;}int main(){    while(~scanf("%d%d",&n,&m))    {        bool flag = 0;        for(int i=0; i<n; i++)        {           scanf("%s",ma[i]);           if(flag)            continue;            for(int j=0; j<m; j++)            {                if(ma[i][j]=='a')                {                     sx=i;sy=j;flag = true;                     break;                }            }        }        memset(vis,0,sizeof(vis));        BFS(sx,sy);    }    return 0;}

Bidirectional BFS

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>const int N = 210;using namespace std;int mapp[N][N];int vis[N][N];struct node{    int x,y;};int n,m;char ma[210][210];int mv[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};int dis[N][N];void BFS(int sx,int sy,int ex,int ey){    queue<node>q;    node t,f;    memset(vis,0,sizeof(vis));    memset(dis,0,sizeof(dis));    f.x = sx; f.y = sy;    t.x = ex; t.y = ey;    vis[sx][sy] = 1;     vis[ex][ey] = 2;    q.push(f);    q.push(t);    while(!q.empty())    {        t = q.front();        q.pop();        for(int i = 0;i<4;i++)        {            f.x = t.x + mv[i][0];            f.y = t.y + mv[i][1];            if(0<=f.x && f.x <n && 0<=f.y && f.y<m)            {                if(ma[f.x][f.y]=='#') continue ;                if(!vis[f.x][f.y]&& ma[f.x][f.y]=='x')                {                dis[f.x][f.y] = dis[t.x][t.y] + 2;                q.push(f);                vis[f.x][f.y] = vis[t.x][t.y];                }                else if(!vis[f.x][f.y]&& ma[f.x][f.y]=='.')                {                dis[f.x][f.y] = dis[t.x][t.y] + 1;                q.push(f);                vis[f.x][f.y] = vis[t.x][t.y];                }                else if(vis[f.x][f.y]!=vis[t.x][t.y])                {                    printf("%d\n",dis[f.x][f.y]+dis[t.x][t.y] + 1);                    return ;                }            }        }    }    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;}int main(){    int t,sx,sy,ex,ey;    while(~scanf("%d%d",&n,&m))    {       for(int i = 0;i<n;i++)       {           scanf("%s",ma[i]);           for(int j = 0;j<m;j++)           {               if(ma[i][j] == 'a')               {                   sx = i; sy = j;               }               else if(ma[i][j]=='r')               {                   ex = i; ey = j;               }           }       }        BFS(sx,sy,ex,ey);    }    return 0;}


Slag


Acm hdu 1242 Error Correction

Angel's friends want to save Angel not only has one friend

What's wrong with acm hdu 1242?

Add 1 to the number of x steps
My code:
# Include <iostream>
# Include <queue>
# Include <algorithm>
Using namespace std;

Struct Point
{Int x, y, dis;} st;
Int n, m;
Char g [210] [210];
Int d [4] [2] = {-, 0,-, 0 };
Bool OK (int x, int y)
{Return (x> = 0 & x <n & y> = 0 & y <m );}
Int bfs ()
{
Int I;
Point p1, p2;
Bool used [210] [210] = {0 };
Queue <Point> q;
Q. push (st );
Used [st. x] [st. y] = 1;
While (q. size ())
{
P1 = q. front ();
Q. pop ();
If ('R' = g [p1.x] [p1.y])
Return p1.dis;
If ('x' = g [p1.x] [p1.y])
{
G [p1.x] [p1.y] = '.';
P1.dis ++;
Q. push (p1 );
}
Else
For (I = 0; I <4; I ++)
{
P2.x = p1.x + d [I] [0];
P2.y = p1.y + d [I] [1];
If (OK (p2.x, p2.y) & g [p2.x] [p2.y]! = '#'&&! Used [p2.x] [p2.y])
{
P2.dis = p1.dis + 1;
Used [p2.x] [p2.y] = 1;
Q. push (p2 );
}
}
}
Return-1;
}
Int main ()
{
Int I, j;
While (scanf ("% d", & n, & m) = 2)
{
For (I = 0; I <n; I ++)
Scanf ("% s", g [I]);
For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
If ('A' = g [I] [j])
{
St. x = I;
St. y = j;
St. dis = 0;
}
Int ans = bfs ();
If (-1 = ans)
Printf ("Poor ANGEL has to stay in the prison all his life. \ n ");
Else
Printf ("% d & #92 ...... remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.