UVA 1103 Ancient Messages

来源:互联网 发布:java中根号怎么表示 编辑:程序博客网 时间:2024/04/30 08:14

紫书给出了很好的一种思路,对于题目给出的那些图形,按照这些图形中的“洞”的个数就可以分别,那么利用图的深度搜索就可以了,同时注意要注意如果当前的位置存储的字符是‘1’,如果下一个访问到的字符是‘0’,那么计数就要加1,同时对于0的处理和1的处理是不同的,要设置第三种字符‘-’,表示当前位置已经处理过了,具体的实现见源代码:

#include<iostream>#include<vector>#include<string>#include<set>#include<stack>#include<queue>#include<map>#include<algorithm>#include<cmath>#include<iomanip>#include<cstring>#include<sstream>using namespace std;int H, W,cur;map<char, int> ch2in;string dict[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011","1100", "1101", "1110", "1111"};char area[210][210];char Flag[] = {'W','A','K','J','S','D'};bool is_true(int x,int y){return (x >= 0) && (x <= H + 1) && (y >= 0) && (y <= W + 1);}int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };void dfs1(int x,int y){if (!is_true(x, y) || area[x][y] != '0') return;area[x][y] = '-';for (int i = 0; i < 4; i++){dfs1(x+dir[i][0],y+dir[i][1]);}}void dfs2(int x, int y){if (!is_true(x, y) || area[x][y] != '1') return;area[x][y] = '-';for (int i = 0; i < 4; i++){int indexX = x + dir[i][0];int indexY = y + dir[i][1];if (is_true(indexX, indexY)){if (area[indexX][indexY] == '0'){cur++;dfs1(indexX,indexY);}else if (area[indexX][indexY] == '1'){dfs2(indexX,indexY);}}}}int main(){int Case = 0;while (cin >> H >> W){memset(area,'0',sizeof(area));if (H == 0 || W == 0) break;ch2in.clear();W *= 4;for (int i = 1; i <= H; i++){string s;cin >> s;string res = "";for (int j = 0; j < s.size(); j++){int index;if (s[j] >= 'a') index = (s[j] - 'a') + 10;else index = s[j] - '0';res += dict[index];}memcpy(area[i]+1,res.c_str(),res.size());}dfs1(0,0);for (int i = 1; i <= H; i++){for (int j = 1; j <= W; j++){if (area[i][j] == '1'){cur = 0;dfs2(i,j);char it = Flag[cur];if (ch2in.find(it) == ch2in.end()) ch2in[it] = 1;else ch2in[it]++;}}}Case++;cout << "Case " << Case << ": ";for (auto it = ch2in.begin(); it != ch2in.end(); it++){while (it->second){cout << it->first;it->second--;}}cout << endl;}//system("pause");return 0;}


原创粉丝点击