Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=3471
Your new company is building a robot that can hold small lightweight objects. The robot will have the intelligence to determine if an object is light enough to hold. It does this by taking pictures of the object from the 6 cardinal directions, and then inferring an upper limit on the object's weight based on those images. You must write a program to do that for the robot.
You can assume that each object is formed from an N×N×N lattice of cubes, some of which may be missing. Each 1×1×1 cube weighs 1 gram, and each cube is painted a single solid color. The object is not necessarily connected.
Input for the last test case is followed by a line consisting of the number 0.
3 .R. YYR .Y. RYY .Y. .R. GRB YGR BYG RBY GYB GRB .R. YRR .Y. RRY .R. .Y. 2 ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ 0
Maximum weight: 11 gram(s) Maximum weight: 8 gram(s)
怎么删除方块?当两面颜色不同时。
完整代码:
/*0.012s*/ #include<cstdio> #include<cstring> #define REP(i,n) for(int i = 0; i < (n); i++) const int maxn = 10; int n; char pos[maxn][maxn][maxn]; char view[6][maxn][maxn]; char read_char() { char ch; do ch = getchar(); while (ch <= (1 << 5));///排除空格和换行 return ch; } void get(int k, int i, int j, int len, int &x, int &y, int &z) { if (k == 0) { x = len; y = j; z = i; } else if (k == 1) { x = n - 1 - j; y = len; z = i; } else if (k == 2) { x = n - 1 - len; y = n - 1 - j; z = i; } else if (k == 3) { x = j; y = n - 1 - len; z = i; } else if (k == 4) { x = n - 1 - i; y = j; z = len; } else { x = i; y = j; z = n - 1 - len; } // k == 5 } int main(void) { while (scanf("%d", &n), n) { ///注意读取顺序 REP(i, n) REP(k, 6) REP(j, n) view[k][i][j] = read_char(); memset(pos,'#',sizeof(pos));///一开始先假定正方体是满的 REP(k, 6) REP(i, n) REP(j, n) if (view[k][i][j] == '.') { REP(p, n) { int x, y, z; get(k, i, j, p, x, y, z); pos[x][y][z] = '.';///那一排都为空 } } while (true) { bool done = true; REP(k, 6) REP(i, n) REP(j, n) if (view[k][i][j] != '.') { REP(p, n) { int x, y, z; get(k, i, j, p, x, y, z); if (pos[x][y][z] == '.') continue; if (pos[x][y][z] == '#') { pos[x][y][z] = view[k][i][j]; break; } if (pos[x][y][z] == view[k][i][j]) break; pos[x][y][z] = '.';///两面颜色不同,删掉 done = false; } } if (done) break; } int ans = 0; REP(i, n) REP(j, n) REP(k, n) if (pos[i][j][k] != '.') ans++; printf("Maximum weight: %d gram(s)\n", ans); } return 0; }