POJ 1027 “暴力”模拟

强烈抗议POJ卡STL的行径。。。

 

View Code
  1 //Result:wizmann    1027    Accepted    260K    375MS    C++    3250B

  2 #include <cstdio>

  3 #include <cstdlib>

  4 #include <cstring>

  5 #include <algorithm>

  6 #include <iostream>

  7 

  8 using namespace std;

  9 

 10 #define print(x) cout<<x<<endl

 11 #define input(x) cin>>x

 12 #define N 10

 13 #define M 15

 14 #define mul(x) ((x)*(x))

 15 #define INF 1<<25

 16 

 17 

 18 const int mx[]={0,1,0,-1};

 19 const int my[]={-1,0,1,0};

 20 

 21 struct point

 22 {

 23     int x,y;

 24     point(){}

 25     point(int i_x,int i_y)

 26     {

 27         x=i_x;y=i_y;

 28     }

 29 };

 30 

 31 char g[N+4][M+4];

 32 int visit[N+2][M+2];

 33 int score();

 34 point q[2048];

 35 

 36 inline bool inMap(int x,int y)

 37 {

 38     if(x>=0 && x<M && y>=0 && y<N) return true;

 39     else return false;

 40 }

 41 

 42 int bfs(int x,int y,int step,char& color,bool kill=false)

 43 {

 44     q[0]=point(x,y);

 45     color=g[y][x];

 46     int res=0;

 47     int head=0;

 48     int tail=0;

 49     while(head<=tail)

 50     {

 51         point now=q[head];

 52         head++;

 53         if(!visit[now.y][now.x]) res++;

 54         else continue;

 55         visit[now.y][now.x]=step;

 56         for(int i=0;i<4;i++)

 57         {

 58             int nx=now.x+mx[i];

 59             int ny=now.y+my[i];

 60    

 61 

 62             if(inMap(nx,ny) && !visit[ny][nx] && color==g[ny][nx])

 63             {

 64                 q[++tail]=point(nx,ny);

 65             }    

 66         }

 67     }

 68     if(kill)

 69     {

 70         for(int i=0;i<N;i++)

 71         {

 72             for(int j=0;j<M;j++)

 73             {

 74                 if(visit[i][j]==step) g[i][j]='X';

 75             }

 76         }

 77     }

 78     return res;

 79 }

 80 

 81 void kill(int x,int y,int step)

 82 {

 83     char color;

 84     memset(visit,0,sizeof(visit));

 85     bfs(x,y,step,color,true);

 86     //printG();

 87     for(int j=0;j<M;j++)

 88     {

 89         for(int i=N;i>0;i--)

 90         {

 91             for(int k=N-1;k>=0;k--)

 92             {

 93                 if(g[k][j]=='X')

 94                 {

 95                     swap(g[k+1][j],g[k][j]);

 96                 }

 97             }

 98         }

 99     }

100     for(int k=0;k<N;k++)

101     {

102         for(int j=0;j<M-1;j++)

103         {

104             int sum=0;

105             for(int i=0;i<N;i++)

106             {

107                 if(g[i][j]=='X') sum++;

108             }

109             if(sum==N)

110             {

111                 //print("SUM:"<<j);

112                 for(int i=0;i<N;i++)

113                 {

114                     swap(g[i][j],g[i][j+1]);

115                 }

116             }

117         }

118     }

119 }

120 

121 int main()

122 {

123     freopen("input.txt","r",stdin);

124     //freopen("out.txt","w",stdout);

125     int T;

126     input(T);

127     int cas=1;

128     while(T--)

129     {

130         printf("Game %d:\n\n",cas++);

131 

132         for(int j=0;j<M+2;j++) g[N][j]='X';

133 

134         for(int i=N-1;i>=0;i--)

135         {

136             scanf("%s",g[i]);

137             g[i][M]='X';

138         }

139         int score=0;

140         char color;

141         char finalColor;

142         int step=0;

143         int killNum=0;

144         while(1)

145         {

146             //print(step);

147             //printG();

148             step++;

149             memset(visit,0,sizeof(visit));

150             point stPnt=point(INF,INF);

151             int maxNum=-1;

152             for(int j=0;j<M;j++)

153             {

154                 for(int i=0;i<N;i++)

155                 {

156                     if(!visit[i][j] && g[i][j]!='X')

157                     {

158                         int num=bfs(j,i,step,color);

159                         //print(num);

160                         if(num>maxNum)

161                         {

162                             stPnt=point(j,i);

163                             maxNum=num;

164                             finalColor=color;

165                         }

166                         else if(num==maxNum)

167                         {

168                             if(j<stPnt.x)

169                             {

170                                 stPnt=point(j,i);

171                                 finalColor=color;

172                             }

173                             else if(j==stPnt.x && i<stPnt.y)

174                             {

175                                 stPnt=point(j,i);

176                                 finalColor=color;

177                             }

178                         }

179                     }

180                 }

181             }

182             //print(maxNum);

183             if(maxNum<=1) break;

184             else killNum+=maxNum;

185             score+=mul(maxNum-2);

186             //print(stPnt.y<<' '<<stPnt.x);

187             printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",step,stPnt.y+1,stPnt.x+1,maxNum,finalColor,mul(maxNum-2));

188             kill(stPnt.x,stPnt.y,step);

189             

190         }

191         if(M*N-killNum==0) score+=1000;

192         

193         printf("Final score: %d, with %d balls remaining.\n\n",score,M*N-killNum);

194     }

195     return 0;

196 }

你可能感兴趣的:(poj)