zoukankan      html  css  js  c++  java
  • poj 2935 Basic Wall Maze (BFS)

                                                               Basic Wall Maze
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2384   Accepted: 1061   Special Judge

    Description

    In this problem you have to solve a very simple maze consisting of:

    1. a 6 by 6 grid of unit squares
    2. 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
    3. one start and one end marker

    A maze may look like this:

    You have to find a shortest path between the square with the start marker and the square with the end marker. Only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. It is not allowed to leave the grid.

    Input

    The input consists of several test cases. Each test case consists of five lines: The first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the end marker. The third, fourth and fifth lines specify the locations of the three walls. The location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end point followed by the position of its lower end point (in case of a vertical wall). The position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid.

    You may assume that the three walls don’t intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. Moreover, there will always be a valid path from the start marker to the end marker. Note that the sample input specifies the maze from the picture above.

    The last test case is followed by a line containing two zeros.

    Output

    For each test case print a description of a shortest path from the start marker to the end marker. The description should specify the direction of every move (‘N’ for up, ‘E’ for right, ‘S’ for down and ‘W’ for left).

    There can be more than one shortest path, in this case you can print any of them.

    Sample Input

    1 6
    2 6
    0 0 1 0
    1 5 1 6
    1 5 3 5
    0 0

    Sample Output

    NEEESWW

    Source

     
    //其实就是道BFS练习题
    关键是下标是反着来的
    //还有就是那个墙讨论下横竖问题就好了
    #include <iostream> #include <stdio.h> #include <queue> #include <set> #include <vector> #include <string.h> #include <algorithm> using namespace std; int dir[4][2]={-1,0,1,0,0,-1,0,1}; char ch[4]={'N','S','W','E'}; struct node { int x,y; int pre; int id; char c; }; bool v[8][8]; node rc[3][2]; node p[55]; int ex,ey; bool OK(node &a,node &b,int d) { for(int i=0;i<3;i++) if(d<2) { if(rc[i][0].x==rc[i][1].x&&b.y>rc[i][0].y&&b.y<=rc[i][1].y) { if(d==0&&b.x==rc[i][0].x) return false; if(d==1&&a.x==rc[i][0].x) return false; } } else { if(rc[i][0].y==rc[i][1].y&&b.x>rc[i][0].x&&b.x<=rc[i][1].x) { if(d==2&&b.y==rc[i][0].y) return false; if(d==3&&a.y==rc[i][0].y) return false; } } return true; } void output(int &id) { if(p[id].pre==-1) return; output(p[id].pre); printf("%c",p[id].c); } void BFS(int x,int y) { memset(v,0,sizeof(v)); v[x][y]=1; int i,cnt=1; node a,b; queue<node> Q; p[0].pre=-1; p[0].x=x; p[0].y=y; p[0].id=0; Q.push(p[0]); while(!Q.empty()) { a=Q.front(); Q.pop(); for(i=0;i<4;i++) { b.x=a.x+dir[i][0]; b.y=a.y+dir[i][1]; if(b.x<1||b.x>6||b.y<1||b.y>6||v[b.x][b.y]) continue; if(OK(a,b,i)) { v[b.x][b.y]=1; b.c=ch[i]; b.pre=a.id; b.id=cnt; p[cnt]=b; cnt++; Q.push(b); // printf("b=%d %d\n",b.x,b.y); if(b.x==ex&&b.y==ey) { output(b.id); printf("\n");return; } } } } } int main() { int x,y; int i; while(scanf("%d %d",&y,&x),x||y) { scanf("%d %d",&ey,&ex); for(i=0;i<3;i++) scanf("%d %d %d %d",&rc[i][0].y,&rc[i][0].x,&rc[i][1].y,&rc[i][1].x); BFS(x,y); } return 0; }
  • 相关阅读:
    java反射笔记
    Java常见异常类型
    找了这么多毕业设计题目,反而不知道选什么了
    C#中Trim()、TrimStart()、TrimEnd()的用法
    JS bom对象
    HTML随笔
    Sublim text3汉化
    11G RAC ORA-32701
    DB_LINK
    ORA-16957: SQL Analyze time limit interrupt
  • 原文地址:https://www.cnblogs.com/372465774y/p/2754391.html
Copyright © 2011-2022 走看看