uva 795(dfs)
2014-07-27 16:09
344 查看
题解:这道题开始想了很久都没思路,然后在在网上看到别人的做法,将 一个 / 或 \ 放大两倍或三倍的数组来存并用0、1填充,有斜杠的是1,没有是0,即没路和有路,然后用dfs做,但还是不理解为什么是在深搜过程中以判断越界情况来判断是否存在一个圈,所以根据斜杠图去想,因为如果到了数组边缘是无法构成圈的。剩下的就好办了。
#include <stdio.h>
#include <string.h>
const int N = 250;
int row, col, pos
, flag, count, max, step;
int flag1[4] = {0, 0, 1, -1};
int flag2[4] = {1, -1, 0, 0};
void init() {
memset(pos, 0, sizeof(pos));
count = flag = step = 0;
max = -1;
}
void dfs(int x, int y) {
pos[x][y] = 1;
int x0, y0;
for (int i = 0; i < 4; i++) {
x0 = x + flag1[i];
y0 = y + flag2[i];
if (x0 < 0 || x0 >= row || y0 < 0 || y0 >= col) {
flag = 0;
continue;
}
else
if (pos[x0][y0] == 0) {
step++;
dfs(x0, y0);
}
}
}
int main() {
char c;
int t = 1;
while (scanf("%d%d", &col, &row) && (col || row)) {
getchar();
init();
int m = 0, n = 0;
for (int i = 0; i < row; i++, m += 3) {
for (int j = 0, n = 0; j < col + 1; j++, n += 3) {
scanf("%c", &c);
if (c == '\\') {
pos[m]
= 1;
pos[m + 1]
= 0;
pos[m + 2]
= 0;
pos[m][n + 1] = 0;
pos[m + 1][n + 1] = 1;
pos[m + 1][n + 2] = 0;
pos[m][n + 2] = 0;
pos[m + 2][n + 1] = 0;
pos[m + 2][n + 2] = 1;
}
else if (c == '/') {
pos[m]
= 0;
pos[m][n + 1] = 0;
pos[m][n + 2] = 1;
pos[m + 1]
= 0;
pos[m + 2]
= 1;
pos[m][n + 1] = 0;
pos[m + 1][n + 1] = 1;
pos[m + 2][n + 2] = 0;
pos[m + 1][n + 2] = 0;
}
}
}
row = 3 * row;
col = 3 * col;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (pos[i][j] == 0) {
flag = 1;
step = 1;
dfs(i, j);
if (flag == 1) {
count++;
if (step > max)
max = step;
}
}
printf("Maze #%d:\n", t++);
if (count == 0)
printf("There are no cycles.\n\n");
else
printf("%d Cycles; the longest has length %d.\n\n", count, max / 3);
}
return 0;
}
#include <stdio.h>
#include <string.h>
const int N = 250;
int row, col, pos
, flag, count, max, step;
int flag1[4] = {0, 0, 1, -1};
int flag2[4] = {1, -1, 0, 0};
void init() {
memset(pos, 0, sizeof(pos));
count = flag = step = 0;
max = -1;
}
void dfs(int x, int y) {
pos[x][y] = 1;
int x0, y0;
for (int i = 0; i < 4; i++) {
x0 = x + flag1[i];
y0 = y + flag2[i];
if (x0 < 0 || x0 >= row || y0 < 0 || y0 >= col) {
flag = 0;
continue;
}
else
if (pos[x0][y0] == 0) {
step++;
dfs(x0, y0);
}
}
}
int main() {
char c;
int t = 1;
while (scanf("%d%d", &col, &row) && (col || row)) {
getchar();
init();
int m = 0, n = 0;
for (int i = 0; i < row; i++, m += 3) {
for (int j = 0, n = 0; j < col + 1; j++, n += 3) {
scanf("%c", &c);
if (c == '\\') {
pos[m]
= 1;
pos[m + 1]
= 0;
pos[m + 2]
= 0;
pos[m][n + 1] = 0;
pos[m + 1][n + 1] = 1;
pos[m + 1][n + 2] = 0;
pos[m][n + 2] = 0;
pos[m + 2][n + 1] = 0;
pos[m + 2][n + 2] = 1;
}
else if (c == '/') {
pos[m]
= 0;
pos[m][n + 1] = 0;
pos[m][n + 2] = 1;
pos[m + 1]
= 0;
pos[m + 2]
= 1;
pos[m][n + 1] = 0;
pos[m + 1][n + 1] = 1;
pos[m + 2][n + 2] = 0;
pos[m + 1][n + 2] = 0;
}
}
}
row = 3 * row;
col = 3 * col;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (pos[i][j] == 0) {
flag = 1;
step = 1;
dfs(i, j);
if (flag == 1) {
count++;
if (step > max)
max = step;
}
}
printf("Maze #%d:\n", t++);
if (count == 0)
printf("There are no cycles.\n\n");
else
printf("%d Cycles; the longest has length %d.\n\n", count, max / 3);
}
return 0;
}
相关文章推荐
- uva 471(dfs)
- UVALive 5107 dfs暴力搜索
- UVA - 208 Firetruck(floyd+DFS)
- Krypton Factor UVA - 129(DFS回溯)
- 紫书章六例题13 古代象形文字 UVA 1103(3次dfs,好题)
- UVALive 6850 Hidden Plus Signs(dfs)
- UVALive 6663 Count the Regions (离散化,染色,dfs)
- uva 1354 - Mobile Computing(dfs)
- UVa 669 - Defragment(DFS)
- 6_9 天平(UVa839)<二叉树的DFS>
- UVA 572(简单的dfs)
- uva10410 - Tree Reconstruction BFS和DFS重建树
- uva10004 Bicoloring 黑白染色问题,DFS
- UVAlive 3092 无根树->有根树 dfs
- UVA 10452 Marcus, help!( DFS )
- UVa 1343 旋转游戏(dfs+IDA*)
- UVA 439 (骑士移动 第一道DFS 13.07.17)
- uva 10344 23 out of 5(DFS)
- uva--301+dfs回溯穷举
- 文章标题 UVALive 6432 Influence(dfs)