POJ 1099 - Square Ice
http://poj.org/problem?id=1099
概要
ASM という形式にエンコードされた水分子の配置が与えられるので,それをデコードする問題.
ASM は m 列の行列の形で表わされ,
- 1 は水平な水分子
- -1 は垂直な水分子
- 0 はそれ以外
を意味している (サンプルの図を参照).
制約
- 1 <= m <= 11
解法
がんばって実装する.
poj/1099.cc1 #include <iostream> 2 #include <vector> 3 #include <iterator> 4 using namespace std; 5 6 int main(void) 7 { 8 int M; 9 int c = 1; 10 while (cin >> M && M != 0) { 11 vector<vector<int> > matrix(M, vector<int>(M, 0)); 12 for (int i = 0; i < M; i++) { 13 for (int j = 0; j < M; j++) { 14 cin >> matrix[i][j]; 15 } 16 } 17 18 int width = -1; 19 for (int i = 0; i < M; i++) { 20 if (matrix[0][i] == -1) { 21 // vertical 22 width += 1+1; 23 } else if (matrix[0][i] == 1) { 24 // horizontal 25 width += 5+1; 26 } else { 27 width += 3+1; 28 } 29 } 30 const int height = 5+4*(M-2); 31 32 vector<string> square(height, string(width, ' ')); 33 34 for (int i = 0; i < M; i++) { 35 const int col = 4*i; 36 for (int j = 0, pos = 0; j < M; j++, pos++) { 37 if (matrix[i][j] == 1) { 38 // horizontal 39 square[col][pos++] = 'H'; 40 square[col][pos++] = '-'; 41 square[col][pos++] = 'O'; 42 square[col][pos++] = '-'; 43 square[col][pos++] = 'H'; 44 } else if (matrix[i][j] == -1) { 45 // vertical 46 square[col-2][pos] = 'H'; 47 square[col-1][pos] = '|'; 48 square[col][pos] = 'O'; 49 square[col+1][pos] = '|'; 50 square[col+2][pos] = 'H'; 51 pos++; 52 } else { 53 if (pos % 4 != 2) { 54 square[col][pos++] = 'H'; 55 square[col][pos++] = '-'; 56 square[col][pos] = 'O'; 57 if (col >= 2 && square[col-2][pos] == ' ') { 58 square[col-1][pos] = '|'; 59 square[col-2][pos] = 'H'; 60 } else { 61 square[col+1][pos] = '|'; 62 square[col+2][pos] = 'H'; 63 } 64 pos++; 65 } else { 66 square[col][pos] = 'O'; 67 if (col >= 2 && square[col-2][pos] == ' ') { 68 square[col-1][pos] = '|'; 69 square[col-2][pos] = 'H'; 70 } else { 71 square[col+1][pos] = '|'; 72 square[col+2][pos] = 'H'; 73 } 74 pos++; 75 square[col][pos++] = '-'; 76 square[col][pos++] = 'H'; 77 } 78 } 79 } 80 } 81 82 cout << "Case " << c++ << ':' << endl; 83 cout << endl; 84 for (int i = 0; i < width+2; i++) { 85 cout << '*'; 86 } 87 cout << endl; 88 for (int i = 0; i < height; i++) { 89 cout << '*' << square[i] << '*' << endl; 90 } 91 for (int i = 0; i < width+2; i++) { 92 cout << '*'; 93 } 94 cout << endl << endl; 95 } 96 return 0; 97 }