poj1101 The Game

topic:

A game to a board, there are blank and can not walk the grid, each given a board sets the start and end, determine which two can not be together. The space and peripherals can go. If Internet connectivity, find the minimum number of bends.


This problem is something that many pit

  • The length of each board, the width is reversed
  • Start, end of the horizontal and vertical coordinates are reversed
  • Each board to wrap the output

    Thinking

  • The BFS (Priority Queue)
  • Enter the very pit, Refer to code
  • Recording the number of turning sum array for each state
  • Direction enumerate four directions, you can go outside, so pay attention to the border, because it is seeking the number of turn, so to record the last judgment

    Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,sx,sy,ex,ey;
bool map[80][80];
int sum[4][160][160],T1,T2,ans;
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
 struct ss
{
    int x,y,d,num;
    friend bool operator < (ss a,ss b)
    {
        return a.num>b.num;
    }
};
priority_queue<ss>q;

 void bfs()
{
    memset(sum,0xf,sizeof(sum));
    while (!q.empty()) q.pop();
    ss res,tmp;
    res.x=sx; res.y=sy; res.num=1; res.d=0; q.push(res);
    res.d=1; q.push(res);
    res.d=2; q.push(res);
    res.d=3; q.push(res);
    sum[0][sx][sy]=sum[1][sx][sy]=sum[2][sx][sy]=sum[3][sx][sy]=1;
    while (!q.empty())
    {
        res=q.top(); q.pop();
        if (res.x==ex&&res.y==ey)
        {
            ans=res.num;
            return;
        }
        if (res.num>sum[res.d][res.x][res.y]) continue;
        int num1,x,y;
        for (int i=0; i<4; i++)
        {
            if (i==res.d) num1=res.num;
            else num1=res.num+1;
            x=res.x+dir[i][0]; y=res.y+dir[i][1];
            while (x>=0&&x<=n+1&&y>=0&&y<=m+1&&!map[x][y])
            {
                if (num1<sum[i][x][y])
                {
                    sum[i][x][y]=num1;
                    tmp.x=x; tmp.y=y; tmp.num=num1; tmp.d=i;
                    q.push(tmp);
                }
                x=x+dir[i][0]; y=y+dir[i][1];
                if (map[x][y]) break;
            }
            if (x==ex&&y==ey)
                if (num1<sum[i][x][y])
                {
                    sum[i][x][y]=num1;
                    tmp.x=x; tmp.y=y; tmp.num=num1; tmp.d=i;
                    q.push(tmp);
                }
        }
    }
}
 void init()
{
    memset(map,0,sizeof(map));
    char c;
    int j,T2=0;
    while(scanf("%c",&c)==1)
    {
        if(c=='\n')break;
    }
    for(int i=1;i<=n;i++)
    {
        j=0;
        while(scanf("%c",&c)==1)
        {
            j++;
            if(c=='X') map[i][j]=1;
            if(c=='\n') break;
        }
    }
    printf("Board #%d:\n",++T1);
    while(scanf("%d%d%d%d",&sy,&sx,&ey,&ex)==4&&sx)//横纵坐标是反的
    {
        printf("Pair %d:",++T2);
        ans=0;
        bfs();
        if(ans) printf(" %d segments.\n",ans);
        else printf(" impossible.\n");
    }
}

 int main()
{
    while (scanf("%d%d",&m,&n))
    {
        if (!m&&!n) break;
        init(); printf("\n");//坑点,千万注意
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lyxzhz/p/11407121.html