The following program is my ACcepted code for UVA-572 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UVA 572 的AC code!
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw)
If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UVA 572 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This program is for UVA 572 Oil Deposits | |
//Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=513 | |
#include<iostream> | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#define MAX 101 | |
using namespace std; | |
bool Map[MAX][MAX];//true for oil(@), false for absence of oil(*) | |
bool visit[MAX][MAX]; | |
int N, M, ans; | |
void initial() | |
{ | |
memset(Map, false, sizeof(Map)); | |
memset(visit, false, sizeof(visit)); | |
ans = 0; | |
} | |
void printMap() | |
{ | |
for(int i = 0 ; i < M; i++) | |
{ | |
printf("\n"); | |
for(int j = 0; j < N; j++) | |
{ | |
if(Map[i][j]) | |
printf("@"); | |
else | |
printf("#"); | |
} | |
} | |
printf("\n"); | |
} | |
void input() | |
{ | |
char tmpMap[MAX][MAX]; | |
for(int i = 0 ; i < M; i++) | |
gets(tmpMap[i]); | |
for(int i = 0 ; i < M; i++) | |
for(int j = 0; j <= N; j++) | |
{ | |
if(tmpMap[i][j] == '@') | |
Map[i][j] = true; | |
else | |
{ | |
Map[i][j] = false; | |
visit[i][j] = true;//doesn't need to visit | |
} | |
} | |
//printMap(); | |
} | |
void DFS_visit(int x, int y) | |
{ | |
if(visit[x][y])//if visit[x][y] = true; | |
return; | |
if(Map[x][y] == '*') | |
return; | |
visit[x][y] = true; | |
if(x + 1 < M) | |
DFS_visit(x + 1, y); | |
if(x - 1 >= 0) | |
DFS_visit(x - 1, y); | |
if(y + 1 < N) | |
DFS_visit(x, y + 1); | |
if(y - 1 >= 0) | |
DFS_visit(x, y - 1); | |
if(x + 1 < M && y + 1 < N) | |
DFS_visit(x + 1, y + 1); | |
if(x - 1 >= 0 && y + 1 < N) | |
DFS_visit(x - 1, y + 1); | |
if(x + 1 < M && y - 1 >= 0) | |
DFS_visit(x + 1, y - 1); | |
if(x - 1 >= 0 && y - 1 >= 0) | |
DFS_visit(x - 1, y - 1); | |
return; | |
} | |
void DFS() | |
{ | |
for(int i = 0 ; i < M; i++) | |
for(int j = 0; j < N; j++) | |
{ | |
if( !visit[i][j]) | |
{ | |
DFS_visit(i, j); | |
ans ++; | |
} | |
} | |
} | |
int main() | |
{ | |
while(scanf("%d %d\n", &M, &N) != EOF) | |
{ | |
if( N == 0 && M == 0) | |
break; | |
initial(); | |
input(); | |
DFS(); | |
printf("%d\n", ans); | |
} | |
return 0; | |
} |
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!