poj 4001 Xiangqi 模擬 順帶關於模擬的一番吐槽
很多人都覺得模擬題很惡心,各種長代碼。但其實我覺得模擬題是最有美感的一類題,求同存異在模擬中表現的淋漓盡致。
遇到模擬題,不應急於下筆,應首先分析問題共性,抽象出相同的模型。代碼盡量重用。
其次尋找stl中合適的數據結構,實在沒有再進行手寫。因為模擬題範圍一般不大,stl完全可以接受,而且完全麵向對象的想法非常適合模擬各種行為
最後再按模塊編碼,實現目標。
模擬的過程應該是很簡約,具有美感高度抽象的,而絕非雜亂無章,各種複製粘貼的
/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char org[11][10];
const int x[4]={1,0,-1,0};
const int y[4]={0,1,0,-1};
bool killed(int X,int Y)
{
bool nemp;
int i;
int tx,ty,xx,yy;
for(i=0;i<4;i++)
{
tx=X+x[i];ty=Y+y[i];
nemp=false;
if(x[i]) //判斷馬
{
xx=tx+x[i];
if((org[tx][ty-1]==0&&org[xx][ty-1]=='H')||(org[tx][ty+1]==0&&org[xx][ty+1]=='H'))return 1;
}
else
{
yy=ty+y[i];
if((org[tx-1][ty]==0&&org[tx-1][yy]=='H')||(org[tx+1][ty]==0&&org[tx+1][yy]=='H'))return 1;
}
while(tx>=1&&tx<=10&&ty>=1&&ty<=9) //判斷單向
{
if((!nemp&&(org[tx][ty]=='R'||org[tx][ty]=='G'))||(nemp&&org[tx][ty]=='C'))return 1;
if(org[tx][ty]!=0)
{
if(nemp)break;
nemp=true;
}
tx+=x[i]; ty+=y[i];
}
}
return 0;
}
int main()
{
int n,X,Y;
while(~scanf("%d%d%d",&n,&X,&Y)&&n+X+Y)
{
int i,tx,ty;
char c;
memset(org,0,sizeof(org));
for(i=0;i<n;i++)
{
scanf(" %c%d%d",&c,&tx,&ty);
org[tx][ty]=c;
}
bool ok=false;
for(i=0;i<4;i++)
{
tx=X+x[i];ty=Y+y[i];
if(tx<1||tx>3||ty<4||ty>6)continue;
if(killed(tx,ty)==0)ok=true;
}
printf("%s\n",ok?"NO":"YES");
}
}
最後更新:2017-04-03 14:53:52