zoj 2412 Farm Irrigation

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1412

 

最近做了不少图论的DFS和BFS题.这题也是.挺简单的一题,麻烦的地方就是将输进去的字母转化为图.

 

我定义了一个三位数组,把信息都放了进去

 

int d[11][3][3] = {{0, 1, 0, 1, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 1, 1, 0, 1, 0}, {0, 1, 0, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 1, 1, 1, 0, 1, 0}};

 

贴上代码吧,有疑问的请留言哈哈~~

 

/* * 2412.cpp * * Created on: Apr 30, 2010 * Author: wyy * */ #define MaxNum 50 #include<cstdio> using namespace std; struct point { int x, y; }; int d[11][3][3] = {{0, 1, 0, 1, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 1, 1, 0, 1, 0}, {0, 1, 0, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 1, 1, 1, 0, 1, 0}}; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int N, M, ret; int graph[3 * MaxNum][3 * MaxNum]; char ch[MaxNum][MaxNum + 1]; bool visited[3 * MaxNum][3 * MaxNum]; void Init() { ret = 0; for(int i = 0; i < N; ++i) scanf("%s", ch[i]); int t; for(int i = 0; i < N; ++i) for(int j = 0; j < M; ++j) { t = ch[i][j] - 'A'; for(int m = 0; m < 3; ++m) for(int n = 0; n < 3; ++n) graph[3 * i + m][3 * j + n] = d[t][m][n]; } for(int i = 0; i < 3 * N; ++i) for(int j = 0; j < 3 * M; ++j) { if(graph[i][j]) visited[i][j] = false; else visited[i][j] = true; } } bool IsValid(point p) { return (p.x >= 0 && p.y >= 0 && p.x < 3 * N && p.y < 3 * M && !visited[p.x][p.y]); } void DFS(point p) { point m; visited[p.x][p.y] = true; for(int i = 0; i < 4; ++i) { m.x = p.x + dx[i]; m.y = p.y + dy[i]; if(IsValid(m)) DFS(m); } } void Solve() { point p; Init(); for(int i = 0; i < 3 * N; ++i) for(int j = 0; j < 3 * M; ++j) { if(!visited[i][j]) { p.x = i; p.y = j; DFS(p); ++ret; } } printf("%d/n", ret); } int main() { //freopen("input.txt", "r", stdin); while(scanf("%d%d", &N, &M) && N != -1 && M != -1) Solve(); //fclose(stdin); return 0; }

你可能感兴趣的:(Graph,2010)