澳门威尼斯赌场HDU1728 逃离迷宫 BFS

Problem Description
Zjt and Sara will take part in a game, named Game III. Zjt and Sara will
be in a maze, and Zjt must find Sara. There are some strang rules in
this maze. If Zjt move a step, Sara will move a step in opposite
direction.
Now give you the map , you shold find out the minimum steps, Zjt have to
move. We say Zjt meet Sara, if they are in the same position or they are
adjacent .
Zjt can only move to a empty position int four diraction (up, left,
right, down). At the same time, Sara will move to a position in opposite
direction, if there is empty. Otherwise , she will not move to any
position.
The map is a N*M two-dimensional array. The position Zjt stays now is
marked Z, and the position, where Sara stays, is marked E.

M2%10x=N
(x=0,1,2,3….)

HDU 1072 Nightmare(BFS)

L – 逃离迷宫

标题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1728

>  . : empty position
>  X: the wall
>  Z: the position Zjt now stay
>  S: the position Sara now stay

给出N。找到最小的满意条件的M

Nightmare

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 9120Accepted Submission(s): 4389

Problem Description Ignatius had a nightmare last night. He found
himself in a labyrinth with a time bomb on him. The labyrinth has an
exit, Ignatius should get out of the labyrinth before the bomb explodes.
The initial exploding time of the bomb is set to 6 minutes. To prevent
the bomb from exploding by shake, Ignatius had to move slowly, that is
to move from one area to the nearest area(that is, if Ignatius stands on
(x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the
next minute) takes him 1 minute. Some area in the labyrinth contains a
Bomb-Reset-Equipment. They could reset the exploding time to 6
minutes.

Given the layout of the labyrinth and Ignatius’ start position, please
tell Ignatius whether he could get out of the labyrinth, if he could,
output the minimum time that he has to use to find the exit of the
labyrinth, else output -1.

Here are some rules:

  1. We can assume the labyrinth is a 2 array.
  2. Each minute, Ignatius could only get to one of the nearest area, and
    he should not walk out of the border, of course he could not walk on a
    wall, too.
  3. If Ignatius get to the exit when the exploding time turns to 0, he
    can’t get out of the labyrinth.
  4. If Ignatius get to the area which contains Bomb-Rest-Equipment when
    the exploding time turns to 0, he can’t use the equipment to reset the
    bomb.
  5. A Bomb-Reset-Equipment can be used as many times as you wish, if it
    is needed, Ignatius can get to any areas in the labyrinth as many times
    as you wish.
  6. The time to reset the exploding time can be ignore, in other words,
    if Ignatius get to an area which contain Bomb-Rest-Equipment, and the
    exploding time is larger than 0, the exploding time would be reset to

Input The input contains several test cases. The first line of the input
is a single integer T which is the number of test cases. T test cases
follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which
indicate the size of the labyrinth. Then N lines follow, each line
contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the
labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius’ start position, Ignatius starts his escape from this
position.
3: The exit of the labyrinth, Ignatius’ target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the
exploding time by walking to these areas.

Output For each test case, if Ignatius can get out of the labyrinth, you
should output the minimum time he needs, else you should just output

Sample Input

3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0
0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1

Sample Output

4 -1 13

代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

struct node
{
    int x,y;
    int t;
    int step;
    node(int a,int b,int c,int d):x(a),y(b),t(c),step(d) {}
    void Set(int a,int b,int c,int d)
    {
        x=a;
        y=b;
        t=c;
        step=d;
    }
};

int n,m;
int xs,ys,xe,ye;
int mat[10][10];
int vis[10][10];
int dir[4][2]= {-1,0,1,0,0,-1,0,1}; //up down left right

void bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node> Q;
    node first=node(xs,ys,6,0);
    if(first.x==xe&&first.y==ye&&first.t>0)
    {
        printf("%d\n",first.step);
        return;
    }
    Q.push(first);
    vis[first.x][first.y]=first.t;
    while(!Q.empty())
    {
        //printf("----------------\n");
        first=Q.front();
        Q.pop();
        node next=node(0,0,0,0);
        for(int i=0; i<4; i++)
        {
            int tx=first.x+dir[i][0];
            int ty=first.y+dir[i][1];
            if(tx>=n||tx<0||ty>=m||ty<0)
                continue;
            if(mat[tx][ty]==0)
                continue;
            if(vis[tx][ty]+1>=vis[first.x][first.y])
                continue;
            next.Set(tx,ty,first.t-1,first.step+1);
            if(mat[tx][ty]==4)
                next.t=6;
            vis[next.x][next.y]=next.t;
            //printf("%d %d %d %d\n",next.x,next.y,next.t,next.step);
            if(next.x==xe&&next.y==ye&&next.t>0)
            {
                printf("%d\n",next.step);
                return;
            }
            Q.push(next);
        }
    }
    printf("-1\n");
}

int main()
{
    int t;
    scanf("%d",&t);
    //printf("%d\n",t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; int="" j="0;" pre="" return="" xe="i;" xs="i;" ye="j;" ys="j;"><p>
</p><p>
</p>
</n;></node></queue></cstring></cstdio>

1072 Nightmare(BFS) Nightmare Time Limit:
2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9120Accepted Submission(s): 4389 Problem…

Time
Limit:
1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &
%I64u

逃离迷宫
Time Limit:
1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 27250    Accepted Submission(s): 6658

Your task is to find out the minimum steps they meet each other.
 

因为:N的个位仅仅由M的个位决定。N十人由M的个位和拾位决定,N的百位由M的个位11人百位决定。由此及彼

Submit Status

Problem Description

Input
The input contains several test cases. Each test case starts with a line
contains three number N ,M (2<= N <= 20, 2 <= M <= 20 )
indicate the size of the map. Then N lines follows, each line contains M
character. A Z and a S will be in the map as the discription above.
 

凡事从个位開始找寻满足条件的数字就足以

Description

  给定二个m × n (m行,
n列)的迷宫,迷宫中有四个岗位,gloria想从迷宫的二个职责走到其余贰个职位,当然迷宫中约莫地方是空地,gloria可以通过,某个地点是障碍,她必需绕行,从迷宫的一个位置,只可以走到与它周边的4个任务中,当然在行走进度中,gloria不可能走到迷宫外面去。令人恨入骨髓的是,gloria是个没什么方向感的人,因此,她在走路进程中,无法转太多弯了,不然他会晕倒的。大家即使给定的四个地方都以空地,初叶时,gloria所面向的来头未定,她能够选拔4个趋势的别样四个起身,而不算成一遍转弯。gloria能从三个职位走到别的三个职位吗?

Output
For each test case, you should print the minimum steps. “Bad Luck!” will
be print, if they can’t meet each other.
 

#include"stdio.h"
#include "string.h"
#include "math.h"
#include "queue"
using namespace std;
__int64  flag,n;

__int64  make(__int64  x,__int64  dit,__int64  num,__int64  i)
{
    __int64  y,now,j;
    y=1;
    for (j=1;j<dit;j++)
        y*=10;

    now=x+y*i;
    if ((now*now%(y*10)/y)==num) return now;

    return -1;
}
void bfs()
{
    queue<__int64>q;
    __int64  dit,i,cnt,x,now,num;
    q.push(0);
    dit=0;
    flag=-1;
    while (!q.empty())
    {
        cnt=q.size();
        num=n%10;
        dit++;
        n/=10;
        while (cnt--)
        {
            x=q.front();
            q.pop();
            for (i=0; i<=9; i++)
            {
                now=make(x,dit,num,i); // x之前所生成的数字,dit当前搜索到第几位,应该位应该匹配的数字,搜索当前位数字为i
                if (now!=-1)
                {
                    q.push(now);
                    if (n==0)
                    {
                        if (now<flag|| flag==-1)
                            flag=now;
                    }
                }
            }
        }
        if (flag!=-1) return ;

    }
}
int  main()
{
    __int64  t;
    scanf("%I64d",&t);
    while (t--)
    {
        scanf("%I64d",&n);
        if (n==0)
        {
            printf("0\n");
            continue;
        }
        bfs();
        if (flag==-1) printf("None\n");
        else printf("%I64d\n",flag);

    }
    return 0;
}

  给定二个m × n (m行,
n列)的迷宫,迷宫中有多个任务,gloria想从迷宫的多个职位走到别的两个地方,当然迷宫中微微位置是空地,gloria能够穿越,有个别地点是阻碍,她非得绕行,从迷宫的一个岗位,只好走到与它左近的4个职位中,当然在行动进程中,gloria不可能走到迷宫外面去。令人恨入骨髓的是,gloria是个没什么方向感的人,由此,她在走路进程中,无法转太多弯了,不然他会晕倒的。大家假若给定的五个地方都是空地,开头时,gloria所面向的主旋律未定,她能够选择4个趋势的别样一个起身,而不算成二遍转弯。gloria能从二个职分走到其余二个职位吗?

 

Sample Input
4 4
XXXX
.Z..
.XS.
XXXX
4 4
XXXX
.Z..
.X.S
XXXX
4 4
XXXX
.ZX.
.XS.
XXXX

 

Input

Sample Output
1
1
Bad Luck!

Input

  第1表现三个板寸t (1 ≤ t ≤
100),表示测量检验数据的个数,接下去为t组测量试验数据,每组测量检验数据中,
  第1作为七个整数m, n (1 ≤ m, n ≤
100),分别表示迷宫的行数和列数,接下去m行,每行包蕴n个字符,个中字符’.’表示该任务为空地,字符’*’表示该地点为阻碍,输入数据中唯有那二种字符,每组测验数据的末尾一行为5个整数k,
x1, y1, x2, y2 (1 ≤ k ≤ 10,
1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤
m),个中k表示gloria最多能转的弯数,(x1, y1),
(x2,
y2)表示三个任务,当中x1,x2对应列,y1,
y2对应行。

 

  第1行为七个卡尺头t (1 ≤ t ≤
100),表示测验数据的个数,接下去为t组测验数据,每组测量试验数据中, 
  第1表现八个整数m, n (1 ≤ m, n ≤
100),分别表示迷宫的行数和列数,接下去m行,每行李包裹罗n个字符,在那之中字符’.’表示该地点为空地,字符’*’表示该任务为障碍,输入数据中独有那三种字符,每组测量检验数据的结尾一行为5个整数k,
1, y 1, x 2, y 2 (1 ≤ k ≤
10, 1 ≤ x 1, x 2 ≤ n, 1 ≤ y 1,
2 ≤ m),个中k表示gloria最多能转的弯数,(x 1,
1), (x 2,
2)表示五个地方,在那之中x 1,x 2对应列,y 1,
y2对应行。 

 

题意:S和Z同在一迷宫,在那之中一人走一步,其他一位就朝相反的来头走一步,问多少步能够达到一样地点依然三人左近

 

Output

思路:开一个四维数组记录,要小心的是两个人的启幕地点要置为“.”空地

Output

  每组测量检验数据对应该为一行,若gloria能从叁个职位走到其他三个地方,输出“yes”,不然输出“no”。

当然小编一开始是开了八个三个维度数组的,即便过了样比照旧A不了,求教哪位棋手用三维做的点拨下

  每组测量试验数据对应该为一行,若gloria能从一个职位走到别的多少个地方,输出“yes”,不然输出“no”。

 

 

 

Sample Input

[cpp]
#include <stdio.h>  
#include <string.h>  
#include <queue>  
using namespace std; 
 
int m,n; 
int z1,z2,s1,s2; 
char map[25][25]; 
int vis[25][25][25][25]; 
int to[4][2] = {1,0,-1,0,0,-1,0,1};//Z的走法  
int to2[4][2] = {-1,0,1,0,0,1,0,-1};//S走相反路径  
 
struct node 

    int x1,x2,y1,y2,step; 
}; 
 
int check(int x,int y) 

    if(x<0 || y<0 || x>=n || y>=m || map[x][y] ==
‘X’) 
        return 1; 
    return 0; 

 
int judge(int x,int x1,int y,int y1)//符合条件的情景  

    if(x == x1 && y == y1) 
        return 1; 
    if(x == x1+1 && y == y1) 
        return 1; 
    if(x == x1-1 && y == y1) 
        return 1; 
    if(x == x1 && y == y1-1) 
        return 1; 
    if(x == x1 && y == y1+1) 
        return 1; 
    return 0; 

 
int bfs() 

    int i; 
    queue<node> Q; 
    node a,next; 
    memset(vis,0,sizeof(vis)); 
    a.x1 = z1; 
    a.x2 = s1; 
    a.y1 = z2; 
    a.y2 = s2; 
    a.step = 0; 
    vis[z1][z2][s1][s2] = 1;//四维数组记录行动情况  
    Q.push(a); 
    while(!Q.empty()) 
    { 
        a = Q.front(); 
        Q.pop(); 
        if(judge(a.x1,a.x2,a.y1,a.y2)) 
            return a.step; 
        for(i = 0; i<4; i++) 
        { 
            next = a; 
            next.x1 = a.x1+to[i][0]; 
            next.y1 = a.y1+to[i][1]; 
            next.x2 = a.x2+to2[i][0]; 
            next.y2 = a.y2+to2[i][1]; 
            if(check(next.x1,next.y1)) 
                continue; 
            if(check(next.x2,next.y2)) 
            { 
                next.x2 = a.x2; 
                next.y2 = a.y2; 
            } 
            if(vis[next.x1][next.y1][next.x2][next.y2]) 
                continue; 
            vis[next.x1][next.y1][next.x2][next.y2] = 1; 
            next.step = a.step+1; 
            Q.push(next); 
        } 
    } 
    return 0; 

 
int main() 

    while(~scanf(“%d%d”,&n,&m)) 
    { 
        int i,j; 
        for(i = 0; i<n; i++) 
        { 
            scanf(“%s”,map[i]); 
            for(j = 0; map[i][j]; j++) 
            { 
                if(map[i][j] == ‘Z’) 
                { 
                    map[i][j] == ‘.’;//早先地方置为空地  
                    z1 = i; 
                    z2 = j; 
                } 
                else if(map[i][j] == ‘S’) 
                { 
                    map[i][j] == ‘.’; 
                    s1 = i; 
                    s2 = j; 
                } 
            } 
        } 
        int ans; 
        ans = bfs(); 
        if(ans) 
            printf(“%d\n”,ans); 
        else 
            printf(“Bad Luck!\n”); 
    } 
 
    return 0; 

Sample Input

2 5 5 …** *.**. ….. ….. *…. 1 1 1 1 3 5 5 …** *.**.
….. ….. *…. 2 1 1 1 3

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

2 5 5 …** *.**. ….. ….. *…. 1 1 1 1 3 5 5 …** *.**.
….. ….. *…. 2 1 1 1 3

 

int m,n;
int z1,z2,s1,s2;
char map[25][25];
int vis[25][25][25][25];
int to[4][2] = {1,0,-1,0,0,-1,0,1};//Z的走法
int to2[4][2] = {-1,0,1,0,0,1,0,-1};//S走相反路径

 

Sample Output

struct node
{
    int x1,x2,y1,y2,step;
};

Sample Output

no yes

int check(int x,int y)
{
    if(x<0 || y<0 || x>=n || y>=m || map[x][y] == ‘X’)
        return 1;
    return 0;
}

no yes

 

int judge(int x,int x1,int y,int y1)//符合条件的风貌
{
    if(x == x1 && y == y1)
        return 1;
    if(x == x1+1 && y == y1)
        return 1;
    if(x == x1-1 && y == y1)
        return 1;
    if(x == x1 && y == y1-1)
        return 1;
    if(x == x1 && y == y1+1)
        return 1;
    return 0;
}

 

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> P;
typedef pair<pair<int, int>, int> Pair;// pair((横坐标x, 纵坐标y), 转弯次数k) ——转弯次数初值为题目给定,每转弯一次,k就减一,当k减为负时就该退出bfs了
const int MAXN = 107;
const int INF = 0x7FFFFFFF;
char maze[MAXN][MAXN];
//标记该位置是否被访问
bool vis[MAXN][MAXN];
//转弯次数,起点横坐标,起点纵坐标,终点横坐标,终点纵坐标
int k, start_x, start_y, end_x, end_y;
int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void bfs(int m, int n){
    queue<Pair> q;

    q.push(Pair(P(start_x - 1, start_y - 1), k));
    vis[start_x - 1][start_y - 1] = true;
    while(!q.empty()){
        Pair top = q.front();
        q.pop();
        //如果转弯次数超过限制就break(期间队头可能不是终点,但终点一定包含在队列里面)
        if(top.second < 0)
            break;

        for(int i = 0; i < 4; i++){
            int nx = top.first.first + d[i][0], ny = top.first.second + d[i][1];
            //不撞南墙不回头!
            while(0 <= nx && nx < m && 0 <= ny && ny < n && maze[nx][ny] == '.'){
                if(!vis[nx][ny]){
                    vis[nx][ny] = true;
                    q.push(Pair(P(nx, ny), top.second - 1));
                }
                //一直向前
                nx += d[i][0], ny += + d[i][1];
            }
        }
    }
}

int main(){
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while(T--){
        int m, n;
        cin >> m >> n;
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                cin >> maze[i][j];
        cin >> k >> start_y >> start_x >> end_y >> end_x;
        bfs(m, n);
        cout << (vis[end_x - 1][end_y - 1] ? "yes" : "no") << endl;
    }
}

int bfs()
{
    int i;
    queue<node> Q;
    node a,next;
    memset(vis,0,sizeof(vis));
    a.x1 = z1;
    a.x2 = s1;
    a.y1 = z2;
    a.y2 = s2;
    a.step = 0;
    vis[z1][z2][s1][s2] = 1;//四维数组记录行动景况
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(judge(a.x1,a.x2,a.y1,a.y2))
            return a.step;
        for(i = 0; i<4; i++)
        {
            next = a;
            next.x1 = a.x1+to[i][0];
            next.y1 = a.y1+to[i][1];
            next.x2 = a.x2+to2[i][0];
            next.y2 = a.y2+to2[i][1];
            if(check(next.x1,next.y1))
                continue;
            if(check(next.x2,next.y2))
            {
                next.x2 = a.x2;
                next.y2 = a.y2;
            }
            if(vis[next.x1][next.y1][next.x2][next.y2])
                continue;
            vis[next.x1][next.y1][next.x2][next.y2] = 1;
            next.step = a.step+1;
            Q.push(next);
        }
    }
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
using namespace std;
const int MAXN=105;
const int MAXM=105;
int n,m,i,T;
int done[MAXN][MAXN];
int sx,sy,ex,ey;
char map[MAXN][MAXN];
int dx[]={0,0,1,-1};//右左下上
int dy[]={1,-1,0,0};

struct node
{
    int x,y,w,dir;
    node(int xx=0,int yy=0,int ww=0,int di=0)
    {
        x=xx;y=yy;w=ww;dir=di;
    }
};
bool is_ill(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)return 1;
    return 0;
}
int bfs()
{
    int i;
    queue<node>q;
    memset(done,0,sizeof(done));  
    q.push(node(sx,sy,-1,-1));
    done[sx][sy]=1;
    while(!q.empty())
    {
        node e=q.front();q.pop();
        if(e.w>=T)continue;
        for(i=0;i<4;i++)
        {
            int curx=e.x+dx[i];  
            int cury=e.y+dy[i];
            int curw=e.w+1;

            while(!is_ill(curx,cury)&&map[curx][cury]!='*')      
            {  
                if(curx==ex&&cury==ey)return 1;
                if(done[curx][cury]){curx+=dx[i];cury+=dy[i];continue;}
                q.push(node(curx,cury,curw));      
                done[curx][cury]=1;
                curx+=dx[i];
                cury+=dy[i];
            }  
        } 
    }
    return 0;
}
int main()
{
    //freopen("123.txt","r",stdin);
    int N;
    cin>>N;
    while(N--)
    {
        scanf("%d%d",&n,&m);
        gets(map[0]);
        for(i=0;i<n;i++)
            gets(map[i]);
        scanf("%d%d%d%d%d",&T,&sy,&sx,&ey,&ex);
        sy-=1;sx-=1;
        ex-=1;ey-=1;
        if(bfs())printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

int main()
{
    while(~scanf(“%d%d”,&n,&m))
    {
        int i,j;
        for(i = 0; i<n; i++)
        {
            scanf(“%s”,map[i]);
            for(j = 0; map[i][j]; j++)
            {
                if(map[i][j] == ‘Z’)
                {
                    map[i][j] == ‘.’;//起先地点置为空地
                    z1 = i;
                    z2 = j;
                }
                else if(map[i][j] == ‘S’)
                {
                    map[i][j] == ‘.’;
                    s1 = i;
                    s2 = j;
                }
            }
        }
        int ans;
        ans = bfs();
        if(ans)
            printf(“%d\n”,ans);
        else
            printf(“Bad Luck!\n”);
    }

    return 0;
}

 

 

Description Zjt and Sara will take part
in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must
find Sara. There are some strang rules in this maze. If Zjt…

发表评论

电子邮件地址不会被公开。 必填项已用*标注