标签:os io for ar 代码 amp sp line on
很简单的模拟题,不多说直接上代码,要注意输出的格式和换行符的接收。
#include <cstdio> #include <iostream> #include <cstring> using namespace std; char maps[5][7]; struct point { int x; int y; }; int main() { #ifdef LOCAL freopen("in","r",stdin); freopen("out","w",stdout); #endif // LOCAL bool line = false; int cnt = 0; while(gets(maps[0])) { if (strcmp(maps[0], "Z") == 0) break; gets(maps[1]); gets(maps[2]); gets(maps[3]); gets(maps[4]); point pos; for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { if(maps[i][j] == ' ') { pos.x = i; pos.y = j; break; } } } char command [1000]; bool flsh = true; bool exit_koro = false; while ( !exit_koro && gets (command)) { for ( int i = 0; command [i] != 0; i++ ) { if ( command [i] == '0' || !flsh ) { exit_koro = true; break; } switch(command[i]) { case 'A': if(flsh && pos.x - 1 >= 0) { maps[pos.x][pos.y] = maps[pos.x - 1][pos.y]; maps[pos.x - 1][pos.y ] = ' '; pos.x -= 1; } else { flsh = false; } break; case 'B': if(flsh && pos.x + 1 <= 4) { maps[pos.x][pos.y] = maps[pos.x + 1][pos.y]; maps[pos.x + 1][pos.y] = ' '; pos.x += 1; } else { flsh = false; } break; case 'R': if(flsh && pos.y + 1 <= 4) { maps[pos.x][pos.y] = maps[pos.x][pos.y + 1]; maps[pos.x][pos.y + 1] = ' '; pos.y += 1; } else { flsh = false; } break; case 'L': if(flsh && pos.y - 1 >= 0) { maps[pos.x][pos.y] = maps[pos.x][pos.y - 1]; maps[pos.x][pos.y - 1] = ' '; pos.y -= 1; } else { flsh = false; } break; default: break; } } } if (line) printf("\n"); line = true; printf("Puzzle #%d:\n", ++cnt); if(flsh) { for ( int i = 0; i < 5; i++ ) { printf ("%c %c %c %c %c\n", maps [i] [0], maps [i] [1], maps [i] [2], maps [i] [3], maps [i] [4]); } } else printf ("This puzzle has no final configuration.\n"); } return 0; }
标签:os io for ar 代码 amp sp line on
原文地址:http://blog.csdn.net/szq123456123/article/details/38869821