POJ 1101(BFS)
来源:互联网 发布:java中的设计模式 编辑:程序博客网 时间:2024/06/09 17:38
The Game
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9905 Accepted: 3017
Description
One morning, you wake up and think: "I am such a good programmer. Why not make some money?'' So you decide to write a computer game.
The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.
One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:
It consists of straight segments, each one being either horizontal or vertical.
It does not cross any other game pieces.
(It is allowed that the path leaves the board temporarily.)
Here is an example:
The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.
The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.
The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.
One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:
It consists of straight segments, each one being either horizontal or vertical.
It does not cross any other game pieces.
(It is allowed that the path leaves the board temporarily.)
Here is an example:
The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.
The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.
Input
The input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a "X" if there is a game piece at this location, and a space if there is no game piece.
Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing "0 0 0 0".
The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.
Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing "0 0 0 0".
The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.
Output
For each board, output the line "Board #n:", where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with "Pair m: ", where m is the number of the pair (starting the count with 1 for each board). Follow this by "ksegments.", where k is the minimum number of segments for a path connecting the two game pieces, or "impossible.", if it is not possible to connect the two game pieces as described above.
Output a blank line after each board.
Output a blank line after each board.
Sample Input
5 4XXXXXX XXXX X XXX 2 3 5 31 3 4 42 3 3 40 0 0 00 0
Sample Output
Board #1:Pair 1: 4 segments.Pair 2: 3 segments.Pair 3: impossible.
Source
Mid-Central European Regional Contest 1999
题意:问点X到点Y,最少需要使用几条线段,就是问你最少拐几次弯
题解:BFS,保存上次的走的状态,与当前的比较,如果不同就使拐弯次数+1。然后更新最小的输出
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<string> #include<bitset> #include<utility> #include<functional> #include<iomanip> #include<sstream> #include<ctime> using namespace std;#define N int(1e2) #define inf int(0x3f3f3f3f) #define mod int(1e9+7) typedef long long LL;#ifdef CDZSC #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif char s[N][N];int n,m,vis[N][N];int vx[] = { 0, 0, 1, -1 };int vy[] = { 1, -1, 0, 0 };struct point{int dicx, dicy, step, x, y;point(int _dx, int _dy, int _s, int _x, int _y) : dicx(_dx), dicy(_dy), step(_s), x(_x), y(_y){}point(){}};bool judge(int x, int y){return (x >= 0 && x <= n + 1 && y >= 0 && y <= m + 1);}int bfs(int a, int b, int c, int d){int ans = inf;point top;memset(vis, 0, sizeof(vis));queue<point>q;q.push(point(0, 0, 0, a, b));vis[a][b] = 1;while (!q.empty()){int tx, ty, dx, dy, seg;top = q.front(); q.pop();if (top.x == c&&top.y == d){ans = min(ans, top.step);}for (int i = 0; i < 4; i++){dx = top.dicx;dy = top.dicy;tx = top.x+vx[i];ty = top.y+vy[i];seg = top.step;if (!judge(tx, ty)){continue;}if (vis[tx][ty] || s[tx][ty] == 'X'){continue;}vis[tx][ty] = 1;if (vx[i] == dx&&vy[i] == dy){q.push(point(dx, dy, seg, tx, ty));}else{q.push(point(vx[i], vy[i], seg+1, tx, ty));}}}return ans;}int main(){#ifdef CDZSC freopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout); int _time_jc = clock();#endifint a,b,c,d,cass=0;while (~scanf("%d%d", &m, &n)){int cas = 0;if (n == 0 && m == 0)break;getchar();for (int i = 1; i <= n; i++){gets(s[i] + 1);//puts(s[i]+1);}for (int i = 0; i <= m+1; i++){s[0][i] = '0';s[n + 1][i] = '0';}for (int i = 0; i <= n+1; i++){s[i][0] = '0';s[i][m + 1] = '0';s[i][m + 2] = '\0';}if (cass)puts("");printf("Board #%d:\n", ++cass);while (~scanf("%d%d%d%d", &a, &b, &c, &d)){if (a == 0 && b == 0 && c == 0 && d == 0)break;s[d][c] = '0';int ans = bfs(b, a, d, c);if (ans!=inf)printf("Pair %d: %d segments.\n", ++cas,ans);elseprintf("Pair %d: impossible.\n", ++cas);s[d][c] = 'X';}}#ifdef CDZSC debug("time: %d\n", int(clock() - _time_jc));#endif return 0;}
0 0
- POJ 1101(BFS)
- POJ 1101 The Game (bfs)
- POJ 题目1101 The Game(bfs)
- poj--1101--The Game(bfs)
- POJ-2056(BFS)
- POJ-1198(BFS)
- POJ 3984(bfs)
- POJ 3278(BFS)
- POJ 3126 (BFS)
- POJ 3414 (BFS)
- POJ 2251(BFS)
- poj 2415(BFS)
- POJ - 3278(BFS)
- POJ 3278(bfs)
- poj 1101 The Game BFS
- poj 1101 The Game(bfs)
- poj 1101 The Game bfs
- [POJ 1101]The Game Bfs
- 简单的Kafka0.8消费者程序
- poj 1163 The Triangle --dp
- 延云YDB&&YA100安装部署文档
- Android源码解析——AsyncTask
- 人生不设限 没有不可能
- POJ 1101(BFS)
- 【Linux学习笔记五】用户与用户组管理
- 20160501
- 六 SQLite 数据库存储数据
- design pattern
- android 创建简单通用工程
- 使用android创建安卓项目工程或者创建安卓测试项目工程命令详解
- 六、类和对象
- IDEA导入web项目时Tomcat的配置