原题:清华大学2017机试样题
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
char map[1000][1000];
bool flag[1000][1000]={false};
bool check[1000][1000]={false};//1表示已经被探明
int num[1000][1000];
bool newcheck[1000][1000]={false};
int newchecknum;
int checknumsum=0;
int safe=1;
int n,m;
int a[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int allxing=0;
void getnum()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int tem=0;
for(int k=0;k<8;k++)
{
if(map[i+a[k][0]][j+a[k][1]]=='*')
{
tem++;
}
}
num[i][j]=tem;
if(map[i][j]=='.')
{
allxing++;
}
}
}
}
void flaging(int x,int y)
{
if(check[x][y]==true)
{
printf("swept\n");
}
else if(check[x][y]==false &&flag[x][y]==false)
{
flag[x][y]=true;
printf("success\n");
}
else
{
flag[x][y]=false;
printf("cancelled\n");
}
}
void newchecking(int x,int y)
{
if(check[x][y]==false)
{
newcheck[x][y]=true;
check[x][y]=true;
checknumsum++;
newchecknum++;
}
for(int k=0;k<8;k++)
{
if(flag[x+a[k][0]][y+a[k][1]]==true)
{
flag[x+a[k][0]][y+a[k][1]]==false;
}
if((x+a[k][0])>=1&&(x+a[k][0])<=n&&(y+a[k][1])>=1&&(y+a[k][1])<=m&&num[x+a[k][0]][y+a[k][1]]!=0&&check[x+a[k][0]][y+a[k][1]]==false)
{
newcheck[x+a[k][0]][y+a[k][1]]=true;
check[x+a[k][0]][y+a[k][1]]=true;
newchecknum++;
checknumsum++;
}
if((x+a[k][0])>=1&&(x+a[k][0])<=n&&(y+a[k][1])>=1&&(y+a[k][1])<=m&&num[x+a[k][0]][y+a[k][1]]==0&&check[x+a[k][0]][y+a[k][1]]==false)
{
newchecking(x+a[k][0],y+a[k][1]);
}
}
}
void sweep(int x,int y)
{
if(check[x][y]==true)
{
printf("swept\n");
}
else if(flag[x][y]==true)
{
printf("flagged\n");
}
else
{
if(map[x][y]=='*')
{
safe=0;
printf("boom\n");
}
else if(num[x][y]>0)
{
check[x][y]=true;
checknumsum++;
printf("1 cell(s) detected\n");
printf("%d %d %d\n",x,y,num[x][y]);
}
else if(num[x][y]==0)
{
newchecknum=0;
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
{
newcheck[i][j]=false;
}
}
newchecking(x,y);
printf("%d cell(s) detected\n",newchecknum);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(newcheck[i][j]==true)
{
printf("%d %d %d\n",i,j,num[i][j]);
}
}
}
}
}
}
void dsweep(int x,int y)
{
int flagnum=0;
for(int k=0;k<8;k++)
{
if(flag[x+a[k][0]][y+a[k][1]]==true)
{
flagnum++;
}
}
if(check[x][y]==false)
{
printf("not swept\n");
}
else if(num[x][y]==0||flagnum!=num[x][y])
{
printf("failed\n");
}
else
{
newchecknum=0;
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
{
newcheck[i][j]=false;
}
}
for(int k=0;k<8;k++)
{
int temx=x+a[k][0];
int temy=y+a[k][1];
if(flag[temx][temy]==false)
{
if(map[temx][temy]=='*')
{
printf("boom\n");
safe=0;
break;
}
else if(check[temx][temy]==false&&map[temx][temy]=='.')
{
newcheck[temx][temy]=true;
check[temx][temy]=true;
newchecknum++;
checknumsum++;
}
}
}
if(safe==1)
{
printf("%d cells detected\n",newchecknum);
for(int kk=0;kk<8;kk++)
{
if(newcheck[x+a[kk][0]][y+a[kk][1]]==true)
{
printf("%d %d %d\n",x+a[kk][0],y+a[kk][1],num[x+a[kk][0]][y+a[kk][1]]);
}
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>map[i][j];
}
}
getnum();
string a;
int x,y;
int step=0;
string b="Sweep";
while(cin>>a)
{
if(a==b)
{
cin>>x>>y;
sweep(x,y);
step++;
cout<<"num of checked:"<<checknumsum<<endl;
}
else if(a=="Flag")
{
cin>>x>>y;
flaging(x,y);
step++;
}
else if(a=="DSweep")
{
cin>>x>>y;
dsweep(x,y);
step++;
cout<<"num of checked:"<<checknumsum<<endl;
}
if(safe==0)
{
printf("game over\n");
printf("total step: %d",step);
return 0;
}
if(checknumsum==allxing)
{
printf("finish\n");
printf("total step: %d",step);
return 0;
}
if(a=="Quit")
{
printf("give up\n");
printf("total step: %d",step);
return 0;
}
}
}
有很多细节需要注意:
1、新探明
2、连锁
3、输出