UVa 784 Maze Exploration

方法:dfs(flood fill)

dfs

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71
 72 vector<string> v;
 73 int dx[] = {-1,0,1,0};
 74 int dy[] = {0,1,0,-1};
 75 void dfs(int x, int y)
 76 {
 77     v[x][y] = ‘#‘;
 78     for (int d = 0; d < 4; ++d)
 79     {
 80         int nx = x+dx[d], ny = y+dy[d];
 81
 82         if (nx < 0 || ny < 0 || nx >= v.size()-1 || ny >= v[nx].length() || v[nx][ny] != ‘ ‘)
 83         {continue;}
 84
 85         dfs(nx, ny);
 86     }
 87 }
 88 int main()
 89 {
 90     ios::sync_with_stdio(false);
 91     cin.tie(0);
 92     int T;  cin >> T;
 93     string line;
 94     getline(cin, line);
 95     for (int kase = 1; kase <= T; ++kase)
 96     {
 97         v.clear();
 98         while (getline(cin, line))
 99         {
100             v.push_back(line);
101             if (line[0] == ‘_‘) break;
102
103         }
104         bool done = false;
105         for (int i = 0; !done && i < v.size()-1; ++i) for (int j = 0; !done && j < v[i].length(); ++j)
106             if (v[i][j] == ‘*‘)
107             {
108                 dfs(i, j); done = true;
109             }
110         for (int i = 0; i < v.size(); ++i) cout << v[i] << newline;
111     }
112 }

时间: 2024-10-13 01:43:42

UVa 784 Maze Exploration的相关文章

uva 784 Maze Exploration(DFS遍历图)

uva 784 Maze Exploration A maze(迷宫) of rectangular(矩形的) rooms is represented on a two dimensional(空间的) grid as illustrated(阐明) in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same charact

uva 784 Maze Exploration(简单dfs)

这道题看上去很麻烦,什么迷宫啊,门之类的,其实挺简单的,就是让把与 * 连通的都置为 # 包括 * , 直接dfs就可以了,不过我wa了好多次...最后竟然是多读了一个换行,忘了加getchar()了,gets()函数 会把缓冲区里面的换行给读进去的...应该把换行去掉,血的教训啊... 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int dx[4]={-1,0,0,1}; int dy[4]=

784 - Maze Exploration

#include <stdio.h> #include <string.h> char maze[50][100]; void search(int i,int j) { if(maze[i][j]!='*' && maze[i][j]!='_' && maze[i][j]!=' ') return; maze[i][j]='#'; search(i-1,j-1); search(i-1,j); search(i-1,j+1); search(i,j

Maze Exploration UVA 784 (简单的深度优先搜索)

说说: 这道题的题意,事先题目给你如下图所示: XXXXXXXXX X         X       X X    *             X X        X        X XXXXXXXXX X        X X        X X        X XXXXX 可以把以上图像想象成一间房子,字符'X'是围墙,你现在所处的位置是'*'所在的地方,要求你将'*'周围的空白,包括'*'本身全部用'#代替.注意不能穿越围墙.其实就是最最基础的深度遍历的问题,只要找到'*'所在的

UVa 784 迷宫探索

题意:迷宫里有X作为墙,空格为可通过,*标记起点.然后需要你现在来刷墙,刷那些由起点可达的房间的墙,即找由起点可达的连通块.这里题目说墙是除几个字符之外的可打印字符,没有说是X.做题时当成了X,结果AC了~题目说到墙都是3格子宽1格子厚,好像也没什么用~ 思路:输入时找到起点并记下,然后由起点深搜. 注意:从网站拷贝样例数据到txt时,它自动地少了很多空格,导致这个测试数据时不符合题意的.(不容易发现复制粘贴过程中少了很多空格~)这样的话,如果是我代码中dfx中的注释掉的那个 if 语句,程序就

UVa 10377 - Maze Traversal

題目:一個機器人在迷宮中行走,它的指令是方向控制(前進.左轉.右轉),給你初始位置和一些指令: 問最後停在那個位置. 分析:模擬.直接模擬即可,注意一下細節. 如果,不能行走(邊界或者是墻壁)則停在原地,指令是到達'Q'结束. 說明:注意多组输出之间有一个换行,不然会WA,UVa这个PE算WA╮(╯▽╰)╭. #include <cstring> #include <cstdio> char maps[62][62]; char towards[5] = "NESW&qu

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVA 784-Maze Exploration(dfs)

Maze Exploration A maze of rectangular rooms is represented on a two dimensional grid as illustrated in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same character which can be any printa

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor