模拟,用高内聚低耦合的思想来写模拟题还是很好的,提高了函数的可重用性,程序的可读性,正确性,总而言之,写函数麻烦,总比debug麻烦来的好。

  1 #include<cstdio>
  2 const int M=16;
  3 struct point{
  4     int x,y;
  5 }p[M];
  6 char has[M][M],op[M];
  7 bool vis[M][M];
  8 int dx[]={-1,1,0,0};
  9 int dy[]={0,0,-1,1};
 10 bool insidemap(const point &a){
 11     if(a.x>=1&&a.x<=10&&a.y>=1&&a.y<=9) return true;return false;
 12 }
 13 void flag(const point &a){
 14     vis[a.x][a.y]=true;
 15 }
 16 bool had(const point &a){
 17     if(has[a.x][a.y]!='.') return true;return false;
 18 }
 19 void step(point &a,int dir){
 20     a.x+=dx[dir];
 21     a.y+=dy[dir];
 22 }
 23 void solveG(point a){
 24     while(true){
 25         step(a,0);
 26         if(!insidemap(a)) return ;
 27         flag(a);
 28         if(had(a)) return ;
 29     }
 30 }
 31 void solveR(const point &b){
 32     for(int i=0;i<4;i++){
 33         point a=b;
 34         while(true){
 35             step(a,i);
 36             if(!insidemap(a)) break;
 37             flag(a);
 38             if(had(a)) break;
 39         }
 40     }
 41 }
 42 void solveH(const point &b){
 43     for(int i=0;i<4;i++){
 44         point a=b;
 45         step(a,i);
 46         if(had(a)) continue;
 47         step(a,i);
 48         if(i<2){
 49             for(int j=2;j<4;j++){
 50                 point c=a;
 51                 step(c,j);
 52                 if(insidemap(c)) flag(c);
 53             }
 54         }
 55         else{
 56             for(int j=0;j<2;j++){
 57                 point c=a;
 58                 step(c,j);
 59                 if(insidemap(c)) flag(c);
 60             }
 61         }
 62     }
 63 }
 64 void solveC(const point &b){
 65     for(int i=0;i<4;i++){
 66         point a=b;
 67         while(true){
 68             step(a,i);
 69             if(!insidemap(a)||had(a)) break;
 70         }
 71         while(true){
 72             step(a,i);
 73             if(!insidemap(a)) break;
 74             flag(a);
 75             if(had(a)) break;
 76         }
 77     }
 78 }
 79 bool insidehouse(const point &a){
 80     if(a.x>=1&&a.x<=3&&a.y>=4&&a.y<=6) return true;return false;
 81 }
 82 bool judge(const point &b){
 83     for(int i=0;i<4;i++){
 84         point a=b;
 85         step(a,i);
 86         if(insidehouse(a)&&!vis[a.x][a.y]) return false;
 87     }
 88     return true;
 89 }
 90 int main(){
 91     int n;
 92     while(~scanf("%d%d%d",&n,&p[0].x,&p[0].y),n|p[0].x|p[0].y){
 93         for(int i=1;i<=10;i++){
 94             for(int j=1;j<=9;j++){
 95                 has[i][j]='.';
 96                 vis[i][j]=false;
 97             }
 98         }
 99         for(int i=1;i<=n;i++){
100             scanf("%s%d%d",op,&p[i].x,&p[i].y);
101             has[p[i].x][p[i].y]=op[0];
102         }
103         for(int i=1;i<=n;i++){
104             op[0]=has[p[i].x][p[i].y];
105             if(op[0]=='G'){
106                 solveG(p[i]);
107             }
108             else if(op[0]=='R'){
109                 solveR(p[i]);
110             }
111             else if(op[0]=='H'){
112                 solveH(p[i]);
113             }
114             else{
115                 solveC(p[i]);
116             }
117         }
118         puts(judge(p[0])?"YES":"NO");
119     }
120     return 0;
121 }
View Code

相关文章: