POJ 2996-Help Me with the Game(模拟题)
这道题直接模拟就好了,不过我有一点不明白的是,确定棋子的位置时两次的搜索顺序为什么不同?如果说要考虑玩家的视角的话,为什么输出的格式还是一样的呢?呵呵。值得一提的是:为了模拟这道题,我把所有的步骤分布来写了。
1.初步读入数据,略去棋盘框架(input);
2.精确读入棋盘点每个棋子的信息,过滤(filter);
3.搜索(read)
4.输出(print)
不过值得注意的是:这样写代码会很长,看来怎么缩短代码也是值得研究的问题呀。
/**/
/*将各种操作分装在函数中是个不错的手段,
能使你的思路更加清楚,重新阅读代码时也会更加方便;*/
#include < iostream >
#include < cmath >
#include < algorithm >
#include < cassert >
#include < deque >
#include < queue >
using namespace std;
struct node
{
int x;
int y;
} ;
/**/ //////////////////
queue < node > wk;
queue < node > wq;
queue < node > wr;
queue < node > wb;
queue < node > wn;
queue < node > wp;
/**/ //////////////////
queue < node > bk;
queue < node > bq;
queue < node > br;
queue < node > bb;
queue < node > bn;
queue < node > bp;
/**/ ///////////////////
char tempmap[ 100 ][ 100 ];
char chessmap[ 100 ][ 100 ];
bool isletter( char n)
{
if((n>='a'&&n<='z')||(n>='A'&&n<='Z'))
return true;
else
return false;
}
void input()
{
int pos=8;
char temp[100];
int i;
for(i=1;i<=17;i++)
{
if(i%2==1)
gets(temp);
else if(i%2==0)
{
gets(tempmap[pos]);
pos--;
}
}
}
void filter()
{
int i,j;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
chessmap[i][j]=tempmap[i][(j-1)*4+2];
}
}
}
void read()
{
int i,j;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
if(isletter(chessmap[i][j])==true&&(chessmap[i][j]>='A'&&chessmap[i][j]<='Z'))//White grid
{
if(chessmap[i][j]=='K')
{
node temp;
temp.x=j;
temp.y=i;
wk.push(temp);
}
else if(chessmap[i][j]=='Q')
{
node temp;
temp.x=j;
temp.y=i;
wq.push(temp);
}
else if(chessmap[i][j]=='R')
{
node temp;
temp.x=j;
temp.y=i;
wr.push(temp);
}
else if(chessmap[i][j]=='B')
{
node temp;
temp.x=j;
temp.y=i;
wb.push(temp);
}
else if(chessmap[i][j]=='N')
{
node temp;
temp.x=j;
temp.y=i;
wn.push(temp);
}
else if(chessmap[i][j]=='P')
{
node temp;
temp.x=j;
temp.y=i;
wp.push(temp);
}
}
}
}
for(i=8;i>=1;i--)
{
for(j=1;j<=8;j++)
{
if(isletter(chessmap[i][j])==true&&(chessmap[i][j]>='a'&&chessmap[i][j]<='z'))//Black grid
{
if(chessmap[i][j]=='k')
{
node temp;
temp.x=j;
temp.y=i;
bk.push(temp);
}
else if(chessmap[i][j]=='q')
{
node temp;
temp.x=j;
temp.y=i;
bq.push(temp);
}
else if(chessmap[i][j]=='r')
{
node temp;
temp.x=j;
temp.y=i;
br.push(temp);
}
else if(chessmap[i][j]=='b')
{
node temp;
temp.x=j;
temp.y=i;
bb.push(temp);
}
else if(chessmap[i][j]=='n')
{
node temp;
temp.x=j;
temp.y=i;
bn.push(temp);
}
else if(chessmap[i][j]=='p')
{
node temp;
temp.x=j;
temp.y=i;
bp.push(temp);
}
}
}
}
}
void print()
{
printf("White: ");
while(wk.size()!=0)
{
node temp;
temp=wk.front();
wk.pop();
cout<<'K';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wq.size()!=0)
{
node temp;
temp=wq.front();
wq.pop();
cout<<'Q';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wr.size()!=0)
{
node temp;
temp=wr.front();
wr.pop();
cout<<'R';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wb.size()!=0)
{
node temp;
temp=wb.front();
wb.pop();
cout<<'B';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wn.size()!=0)
{
node temp;
temp=wn.front();
wn.pop();
cout<<'N';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wp.size()!=0)
{
node temp;
temp=wp.front();
wp.pop();
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
if(wp.size()==0)
cout<<endl;
else
cout<<',';
}
printf("Black: ");
while(bk.size()!=0)
{
node temp;
temp=bk.front();
bk.pop();
cout<<'K';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bq.size()!=0)
{
node temp;
temp=bq.front();
bq.pop();
cout<<'Q';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(br.size()!=0)
{
node temp;
temp=br.front();
br.pop();
cout<<'R';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bb.size()!=0)
{
node temp;
temp=bb.front();
bb.pop();
cout<<'B';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bn.size()!=0)
{
node temp;
temp=bn.front();
bn.pop();
cout<<'N';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bp.size()!=0)
{
node temp;
temp=bp.front();
bp.pop();
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
if(bp.size()==0)
cout<<endl;
else
cout<<',';
}
}
int main()
{
input();
filter();
read();
print();
system("pause");
return 0;
}
能使你的思路更加清楚,重新阅读代码时也会更加方便;*/
#include < iostream >
#include < cmath >
#include < algorithm >
#include < cassert >
#include < deque >
#include < queue >
using namespace std;
struct node
{
int x;
int y;
} ;
/**/ //////////////////
queue < node > wk;
queue < node > wq;
queue < node > wr;
queue < node > wb;
queue < node > wn;
queue < node > wp;
/**/ //////////////////
queue < node > bk;
queue < node > bq;
queue < node > br;
queue < node > bb;
queue < node > bn;
queue < node > bp;
/**/ ///////////////////
char tempmap[ 100 ][ 100 ];
char chessmap[ 100 ][ 100 ];
bool isletter( char n)
{
if((n>='a'&&n<='z')||(n>='A'&&n<='Z'))
return true;
else
return false;
}
void input()
{
int pos=8;
char temp[100];
int i;
for(i=1;i<=17;i++)
{
if(i%2==1)
gets(temp);
else if(i%2==0)
{
gets(tempmap[pos]);
pos--;
}
}
}
void filter()
{
int i,j;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
chessmap[i][j]=tempmap[i][(j-1)*4+2];
}
}
}
void read()
{
int i,j;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
if(isletter(chessmap[i][j])==true&&(chessmap[i][j]>='A'&&chessmap[i][j]<='Z'))//White grid
{
if(chessmap[i][j]=='K')
{
node temp;
temp.x=j;
temp.y=i;
wk.push(temp);
}
else if(chessmap[i][j]=='Q')
{
node temp;
temp.x=j;
temp.y=i;
wq.push(temp);
}
else if(chessmap[i][j]=='R')
{
node temp;
temp.x=j;
temp.y=i;
wr.push(temp);
}
else if(chessmap[i][j]=='B')
{
node temp;
temp.x=j;
temp.y=i;
wb.push(temp);
}
else if(chessmap[i][j]=='N')
{
node temp;
temp.x=j;
temp.y=i;
wn.push(temp);
}
else if(chessmap[i][j]=='P')
{
node temp;
temp.x=j;
temp.y=i;
wp.push(temp);
}
}
}
}
for(i=8;i>=1;i--)
{
for(j=1;j<=8;j++)
{
if(isletter(chessmap[i][j])==true&&(chessmap[i][j]>='a'&&chessmap[i][j]<='z'))//Black grid
{
if(chessmap[i][j]=='k')
{
node temp;
temp.x=j;
temp.y=i;
bk.push(temp);
}
else if(chessmap[i][j]=='q')
{
node temp;
temp.x=j;
temp.y=i;
bq.push(temp);
}
else if(chessmap[i][j]=='r')
{
node temp;
temp.x=j;
temp.y=i;
br.push(temp);
}
else if(chessmap[i][j]=='b')
{
node temp;
temp.x=j;
temp.y=i;
bb.push(temp);
}
else if(chessmap[i][j]=='n')
{
node temp;
temp.x=j;
temp.y=i;
bn.push(temp);
}
else if(chessmap[i][j]=='p')
{
node temp;
temp.x=j;
temp.y=i;
bp.push(temp);
}
}
}
}
}
void print()
{
printf("White: ");
while(wk.size()!=0)
{
node temp;
temp=wk.front();
wk.pop();
cout<<'K';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wq.size()!=0)
{
node temp;
temp=wq.front();
wq.pop();
cout<<'Q';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wr.size()!=0)
{
node temp;
temp=wr.front();
wr.pop();
cout<<'R';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wb.size()!=0)
{
node temp;
temp=wb.front();
wb.pop();
cout<<'B';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wn.size()!=0)
{
node temp;
temp=wn.front();
wn.pop();
cout<<'N';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(wp.size()!=0)
{
node temp;
temp=wp.front();
wp.pop();
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
if(wp.size()==0)
cout<<endl;
else
cout<<',';
}
printf("Black: ");
while(bk.size()!=0)
{
node temp;
temp=bk.front();
bk.pop();
cout<<'K';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bq.size()!=0)
{
node temp;
temp=bq.front();
bq.pop();
cout<<'Q';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(br.size()!=0)
{
node temp;
temp=br.front();
br.pop();
cout<<'R';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bb.size()!=0)
{
node temp;
temp=bb.front();
bb.pop();
cout<<'B';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bn.size()!=0)
{
node temp;
temp=bn.front();
bn.pop();
cout<<'N';
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
cout<<',';
}
while(bp.size()!=0)
{
node temp;
temp=bp.front();
bp.pop();
cout<<(char)('a'+temp.x-1);
cout<<temp.y;
if(bp.size()==0)
cout<<endl;
else
cout<<',';
}
}
int main()
{
input();
filter();
read();
print();
system("pause");
return 0;
}
最后再说两句废话:因为最近写了个队列的模板类,结果做什么题都想用,本来这个队列还想用自己的那个呵,不过比赛的时候可就没那必要了啊,有现成的queue可以用,而且效率和你自己写的那个差不多,为啥不用呢?