1. 程式人生 > >Uva1103 Ancient Messages

Uva1103 Ancient Messages

class 只需要 ons get 覆蓋 print 比較 ges 一個

題意:識別圖中的象形文字。但是,圖形可以任意的拉伸,但不能拉斷。

分析:這種題如果圖形沒有特征是不可做類型的題,不過觀察圖形可以發現每個圖形中的洞的數量是一定的,我們只需要數出每一個封閉圖形的洞數就能知道這是哪個圖形.

雖然知道了原理,但是並不是特別好做,首先我們需要一次dfs將所有圖形旁邊的點全都變為“不可訪問”,然後從每個黑點開始枚舉,向四周擴展,遇到白色的塊就用第一次的dfs函數覆蓋,否則繼續第二次dfs,兩次dfs交錯使用,思路比較巧妙.

#include <cstdio>
#include <cstring>
#include <iostream>
#include 
<algorithm> using namespace std; //0表示空白位置,-1表示不能訪問了. const int maxn = 510; int n, m,kase,a[maxn][maxn],flag[maxn][maxn],cnt,num[maxn]; char s16[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f }; char fuhao[6] = { A, D, J, K, S, W }; int s2[16][4
] = { { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 1, 0 }, { 0, 0, 1, 1 }, { 0, 1, 0, 0 }, { 0, 1, 0, 1 }, { 0, 1, 1, 0 }, { 0, 1, 1, 1 }, { 1, 0, 0, 0 }, { 1, 0, 0, 1 }, { 1, 0, 1, 0 }, { 1, 0, 1, 1 }, { 1, 1, 0, 0 }, { 1, 1, 0, 1 }, { 1, 1, 1, 0 }, { 1, 1, 1, 1 } }; void dfs1(int x,int y) { if (x < 0 || x > n + 1
|| y < 0 || y > m + 1 || a[x][y] != 0) return; a[x][y] = -1; dfs1(x - 1, y); dfs1(x + 1, y); dfs1(x, y - 1); dfs1(x, y + 1); } void dfs2(int x, int y) { if (x < 0 || x > n + 1 || y < 0 || y > m + 1 || a[x][y] == -1) return; if (a[x][y] == 0) { cnt++; dfs1(x, y); return; } a[x][y] = -1; dfs2(x - 1, y); dfs2(x + 1, y); dfs2(x, y - 1); dfs2(x, y + 1); } int main() { while (scanf("%d%d", &n, &m) == 2 && (n || m)) { memset(a, 0, sizeof(a)); memset(num, 0, sizeof(num)); for (int i = 1; i <= n; i++) { getchar(); char ch; int tot = 0; for (int j = 1; j <= m; j++) { scanf("%c", &ch); for (int k = 0; k < 16; k++) { if (ch == s16[k]) { for (int l = 0; l < 4; l++) a[i][++tot] = s2[k][l]; break; } } } } m *= 4; dfs1(0, 0); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[i][j] == 1) { cnt = 0; dfs2(i, j); if (cnt == 0) num[5]++; if (cnt == 1) num[0]++; if (cnt == 2) num[3]++; if (cnt == 3) num[2]++; if (cnt == 4) num[4]++; if (cnt == 5) num[1]++; } printf("Case %d: ", ++kase); for (int i = 0; i <= 5; i++) { while (num[i]--) printf("%c", fuhao[i]); } printf("\n"); } return 0; }

Uva1103 Ancient Messages