uva 657 - The die is cast
2012-07-04 00:25
288 查看
// uva 657 - The die is cast // 题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=598 // 题目大意: 求每个区域内连通X的块数,然后排序输出 // 一开始没注意到要排序,然后wa了,后来在poj找的测试数据,才发现要排序T_T #include <cstdio> #include <iostream> #include <string> #include <cstring> #include <queue> #include <algorithm> using namespace std; int row, column; struct Node{ int x, y; } now_node; queue<Node> node_que; char map[100][100]; bool visit[100][100]; int record[100][100]; int cnt_X, cnt_case, save_pos; int direction[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int save_ans[1300]; bool Judge(char); void Init(); bool Inmap(Node); void BFS(); void DFS(Node); int main(){ while(scanf("%d %d", &column, &row), row || column){ getchar(); Init(); cout << "Throw " << ++cnt_case << endl; for(int i = 0; i < row; i++) gets(map[i]); for(int i = 0; i < row; i++) { for (int j = 0; j < column; j++){ if (Judge(map[i][j]) && !visit[i][j]){ now_node.x = i; now_node.y = j; BFS(); } } } sort(save_ans, save_ans + save_pos); for(int i = 0; i < save_pos; i++){ if(i) cout << " "; cout << save_ans[i]; } cout << endl << endl; } return 0; } void Init(){ memset(map, 0, sizeof(map)); memset(visit, 0, sizeof(visit)); memset(save_ans, 0, sizeof(save_pos)); memset(record, 0, sizeof(record)); save_pos = 0; } bool Inmap(Node node){ return node.x >= 0 && node.y >= 0 && node.x < row && node.y < column; } bool Judge(char element){ return element == '*' || element == 'X'; } void BFS(){ Node tmp_node; cnt_X = 0; visit[now_node.x][now_node.y] = true; node_que.push(now_node); while(node_que.size()) { now_node = node_que.front(); node_que.pop(); for(int i = 0; i < 4; i++){ tmp_node.x = now_node.x + direction[i][0]; tmp_node.y = now_node.y + direction[i][1]; if(Inmap(tmp_node) && Judge(map[tmp_node.x][tmp_node.y]) && !visit[tmp_node.x][tmp_node.y]){ visit[tmp_node.x][tmp_node.y] = true; node_que.push(tmp_node); if(map[tmp_node.x][tmp_node.y] == 'X' && !record[tmp_node.x][tmp_node.y]){ record[tmp_node.x][tmp_node.y] = ++cnt_X; DFS(tmp_node); } } } } save_ans[save_pos++] = cnt_X; } void DFS(Node node){ Node tmp_node; for(int i = 0; i < 4; i++) { tmp_node.x = node.x + direction[i][0]; tmp_node.y = node.y + direction[i][1]; if(Inmap(tmp_node) && !record[tmp_node.x][tmp_node.y] && map[tmp_node.x][tmp_node.y] == 'X'){ record[tmp_node.x][tmp_node.y] = cnt_X; DFS(tmp_node); } } }
相关文章推荐
- UVa 657 - The die is cast
- UVA - 657 The die is cast
- UVa657 The die is cast(DFS)
- UVA 657 The die is cast
- UVa 657 - The die is cast
- UVa 657 The die is cast
- uva 657 The die is cast
- UVA 657 - The die is cast
- UVA 657 The die is cast ——bfs+dfs
- uva657 The die is cast(搜索)
- UVa 657 & POJ 1481 - The die is cast
- UVA 657-The die is cast(双重BFS)
- UVA 657 - The die is cast 深度搜索
- uva 657 The die is cast(DFS遍历图)
- UVA 657 The die is cast
- uva 657 - The die is cast
- UVa 657 - The die is cast
- The die is cast UVA 657
- Uva 657 - The die is cast
- UVA 657 The die is cast