题目的可读版本
有那么几个地方需要注意:
1.使用一个锦囊后,先判无懈可击。
2.如果一个猪出完一个牌后,要从头开始看哪个能出(有可能一次出牌后某只猪跳忠或者跳反了,F,K什么的就可以用了。)
3.游戏结束时,没抓的牌就不用抓了。
4.循环无懈可击
5.行末没空格
昨天打的时候,有个没玩过三国杀的dalao问我能不能无懈杀。。。。
其实这种题完全没有思维含量,只要打之前把所有细节构思好,之后开始码,不要把变量定义混乱,调之前写好注释。。。也就没啥了,一定要有耐心。我也就调了10h左右。。。。
4.9k的代码貌似已经算很短的了。我分别提供带注释和不带注释的代码
其实带注释的代码看起来就像你真的在玩一样,反正都是模拟。。
无注释的:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
int n,m,cnt,sum,nex[15],fro[15];//cnt表示使用到第几张牌,sum表示剩余FP数量
int head[15],tail[15],dead[15],out[15][2005],blood[15],jump[15],nu[15];//jump:1->ZP,2->FP,3->L_AP
char card[2005][2],pig[15][3],hand[15][2005][2];
bool P(int x)
{if(blood[x]!=4){blood[x]++;return 1;}return 0;
}
bool get_D(int x)
{for(int i=head[x];i<=tail[x];i++)if(!out[x][i]&&hand[x][i][0]=='D'){
out[x][i]=1;return 1;}return 0;
}
bool get_J(int x)
{for(int i=head[x];i<=tail[x];i++)if(!out[x][i]&&hand[x][i][0]=='J'){
out[x][i]=1;return 1;}return 0;
}
bool get_K(int x)
{for(int i=head[x];i<=tail[x];i++)if(!out[x][i]&&hand[x][i][0]=='K'){
out[x][i]=1;return 1;}return 0;
}
void dying(int x,int y)//x为进攻者,y为死者
{for(int i=head[y];i<=tail[y];i++)if(!out[y][i]&&hand[y][i][0]=='P'){out[y][i]=1;blood[y]=1;return;}dead[y]=1;nex[fro[y]]=nex[y];fro[nex[y]]=fro[y];if(pig[y][0]=='F')sum--; if(sum==0||blood[1]<=0)return;if(pig[y][0]=='F'){cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];return;}if(pig[y][0]=='Z'&&x==1){nu[x]=0;for(int i=head[x];i<=tail[x];i++)out[x][i]=1;head[x]=tail[x]+1;return;}
}
bool K(int x)
{int y=nex[x];if((jump[y]==1&&pig[x][0]=='F')||(pig[x][0]=='M'&&(jump[y]==2||jump[y]==3))||(pig[x][0]=='Z'&&jump[y]==2)){if(get_D(y)==0){blood[y]--;if(blood[y]<=0)dying(x,y);}if(pig[x][0]=='Z')jump[x]=1;if(pig[x][0]=='F')jump[x]=2;return 1;}return 0;
}
bool J(int star,int x,int ch)
{if(!jump[x]||jump[x]==3)return ch;int y,p=0;for(int i=star;;i=nex[i]){y=i;if(p&&y==star)break;p=1;if(ch==1){if(jump[x]==1&&(pig[y][0]=='Z'||pig[y][0]=='M'))if(get_J(y)){ch=0,jump[y]=1;return J(y,x,ch);}if(jump[x]==2&&pig[y][0]=='F')if(get_J(y)){ch=0,jump[y]=2;return J(y,x,ch);}}else{if(jump[x]==1&&pig[y][0]=='F')if(get_J(y)){ch=1,jump[y]=2;return J(y,x,ch);}if(jump[x]==2&&(pig[y][0]=='Z'||pig[y][0]=='M'))if(get_J(y)){ch=1,jump[y]=1;return J(y,x,ch);}}}return ch;
}
bool F(int x)
{int y=0;if(pig[x][0]=='F'){y=1;jump[x]=2;}if(pig[x][0]=='Z')for(int i=nex[x];;i=nex[i]){if(i==x)break;if(jump[i]==2){y=i;jump[x]=1;break;}}if(pig[x][0]=='M')for(int i=nex[x];;i=nex[i]){if(i==x)break;if(jump[i]==2||jump[i]==3){y=i;break;}}if(!y)return 0;int er=y; if(J(x,y,1)==0){
return 1;} if(x==1&&pig[y][0]=='Z'){blood[y]--;if(blood[y]<=0)dying(x,y);return 1;}while(1){if(get_K(er)==0){blood[er]--;if(blood[er]<=0)dying(er==x?y:x,er);break;}if(er==x)er=y;else er=x;}return 1;
}
void N(int x)
{for(int i=nex[x];;i=nex[i]){int y=i;if(y==x)break; if(J(x,y,1)==0){
continue;} if(get_K(y)==0){blood[y]--;if(blood[y]<=0)dying(x,y);if(!jump[x]&&y==1)jump[x]=3;if(sum==0||dead[1]==1)return;}}return;
}
void W(int x)
{for(int i=nex[x];;i=nex[i]){int y=i;if(y==x)break;if(J(x,y,1)==0){ continue;}if(get_D(y)==0){blood[y]--;if(blood[y]<=0)dying(x,y);if(!jump[x]&&y==1)jump[x]=3;if(sum==0||dead[1]==1)return;}}return;
}
bool play()
{int x=1;while(1){cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];int kill=0;for(int i=head[x];i<=tail[x];i++){int use=0;if(out[x][i])continue;string c=hand[x][i];if(c[0]=='P')if(P(x)){
out[x][i]=1;use=1;}if(c[0]=='K'&&(!kill||nu[x]))if(K(x)){
out[x][i]=1;kill=1;if(nu[x]==1)kill=0;use=1;}if(c[0]=='D')continue;if(c[0]=='F')if(F(x)){
out[x][i]=1;use=1;}if(c[0]=='N'){N(x);out[x][i]=1;use=1;}if(c[0]=='W'){W(x);out[x][i]=1;use=1;}if(c[0]=='J')continue;if(c[0]=='Z'){nu[x]=1;out[x][i]=1;use=1;}if(dead[x])break;if(blood[1]==0)return 0;if(sum==0)return 1;if(use==1)i=head[x]-1;}while(out[x][head[x]]==1)head[x]++;x=nex[x];}
}
void init()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%s",pig[i]);if(pig[i][0]=='F')sum++;for(int j=1;j<=4;j++)scanf("%s",hand[i][j]);tail[i]=4;head[i]=1;fro[i]=i-1;nex[i]=i+1;blood[i]=4;}fro[1]=n;nex[n]=1;jump[1]=1;for(int i=1;i<=m;i++)scanf("%s",card[i]);
}
void jieshu()
{if(play())printf("MP\n");else printf("FP\n");for(int i=1;i<=n;i++){if(dead[i]){printf("DEAD\n");continue;}int sum=0;char hh[2005][3];for(int j=head[i];j<=tail[i];j++)if(!out[i][j])hh[++sum][0]=hand[i][j][0];for(int j=1;j<sum;j++)printf("%c ",hh[j][0]);if(sum==0)printf("\n");else printf("%c\n",hh[sum][0]);}
}
int main()
{init();jieshu();
}
带注释的:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
int n,m,cnt,sum,nex[15],fro[15];//cnt表示使用到第几张牌,sum表示剩余FP数量
int head[15],tail[15],dead[15],out[15][2005],blood[15],jump[15],nu[15];//jump:1->ZP,2->FP,3->L_AP
char card[2005][2],pig[15][3],hand[15][2005][2];
bool P(int x)
{if(blood[x]!=4){
printf("%d use a P\n",x);blood[x]++;return 1;} return 0;
}
bool get_D(int x)
{for(int i=head[x];i<=tail[x];i++)if(!out[x][i]&&hand[x][i][0]=='D'){out[x][i]=1;return 1;}return 0;
}
bool get_J(int x)
{for(int i=head[x];i<=tail[x];i++)if(!out[x][i]&&hand[x][i][0]=='J'){out[x][i]=1;return 1;}return 0;
}
bool get_K(int x)
{for(int i=head[x];i<=tail[x];i++)if(!out[x][i]&&hand[x][i][0]=='K'){out[x][i]=1;return 1;}return 0;
}
void dying(int x,int y)//x为进攻者,y为死者
{for(int i=head[y];i<=tail[y];i++)if(!out[y][i]&&hand[y][i][0]=='P'){printf("%d use a P to save himself\n",y);out[y][i]=1;blood[y]=1;return;}dead[y]=1;nex[fro[y]]=nex[y];fro[nex[y]]=fro[y]; printf("%d dead,killed by %d\n",y,x);if(pig[y][0]=='F')sum--; printf("how about FP:%d\n",sum);if(sum==0||blood[1]<=0)return;if(pig[y][0]=='F'){cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];return;}if(pig[y][0]=='Z'&&x==1){nu[x]=0;for(int i=head[x];i<=tail[x];i++)out[x][i]=1;head[x]=tail[x]+1;return;}
}
bool K(int x)
{int y=nex[x];if((jump[y]==1&&pig[x][0]=='F')||(pig[x][0]=='M'&&(jump[y]==2||jump[y]==3))||(pig[x][0]=='Z'&&jump[y]==2)){printf("%d wants to kill %d\n",x,y); if(get_D(y)==0){blood[y]--;printf("%d blood -1\n",y);if(blood[y]<=0)dying(x,y);}if(pig[x][0]=='Z')jump[x]=1;if(pig[x][0]=='F')jump[x]=2;return 1;}return 0;
}
bool J(int star,int x,int ch)
{if(!jump[x]||jump[x]==3)return ch;int y,p=0; printf("to whom %d and how about now %d\n",x,ch); for(int i=star;;i=nex[i]){y=i;if(p&&y==star)break;p=1;if(ch==1){if(jump[x]==1&&(pig[y][0]=='Z'||pig[y][0]=='M'))if(get_J(y)){printf("%d give a J to %d\n",y,x); ch=0,jump[y]=1;return J(y,x,ch);}if(jump[x]==2&&pig[y][0]=='F')if(get_J(y)){printf("%d give a J to %d\n",y,x); ch=0,jump[y]=2;return J(y,x,ch);}}else{if(jump[x]==1&&pig[y][0]=='F')if(get_J(y)){printf("%d give a J to %d\n",y,x); ch=1,jump[y]=2;return J(y,x,ch);}if(jump[x]==2&&(pig[y][0]=='Z'||pig[y][0]=='M'))if(get_J(y)){printf("%d give a J to %d\n",y,x); ch=1,jump[y]=1;return J(y,x,ch);}}}return ch;
}
bool F(int x)
{int y=0;if(pig[x][0]=='F'){
y=1;jump[x]=2;}if(pig[x][0]=='Z')for(int i=nex[x];;i=nex[i]){if(i==x)break;if(jump[i]==2){
y=i;jump[x]=1;break;}}if(pig[x][0]=='M')for(int i=nex[x];;i=nex[i]){if(i==x)break;if(jump[i]==2||jump[i]==3){
y=i;break;}}if(!y)return 0;int er=y;printf("attarker %d and %d\n",x,y); if(J(x,y,1)==0){ printf("J is used to %d\n",y);return 1;} if(x==1&&pig[y][0]=='Z'){blood[y]--;if(blood[y]<=0)dying(x,y);return 1;}while(1){printf("who %d\n",er); if(get_K(er)==0){printf("loser %d\n",er); blood[er]--;printf("%d blood -1\n",er);if(blood[er]<=0)dying(er==x?y:x,er);break;}if(er==x)er=y;else er=x;}return 1;
}
void N(int x)
{printf("Nan Zhu Run Qin; from %d\n",x); for(int i=nex[x];;i=nex[i]){int y=i;if(y==x)break; printf("to whom %d\n",y); if(J(x,y,1)==0){
printf("J is used to %d\n",y);continue;} if(get_K(y)==0){printf("%d blood -1\n",y); blood[y]--;if(blood[y]<=0)dying(x,y);if(!jump[x]&&y==1)jump[x]=3;if(sum==0||dead[1]==1)return;}}return;
}
void W(int x)
{printf("Wan Jian Qi Fa; from %d\n",x); for(int i=nex[x];;i=nex[i]){int y=i;if(y==x)break;printf("to whom %d\n",y); if(J(x,y,1)==0){
printf("J is used to %d\n",y); continue;} if(get_D(y)==0){printf("%d blood -1\n",y); blood[y]--;if(blood[y]<=0)dying(x,y);if(!jump[x]&&y==1)jump[x]=3;if(sum==0||dead[1]==1)return;}}return;
}
bool play()
{int x=1;while(1){cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];cnt++;if(cnt>m)cnt=m;hand[x][++tail[x]][0]=card[cnt][0];int kill=0;printf("player%d\n",x); for(int j=head[x];j<=tail[x];j++) if(!out[x][j])printf("%s ",hand[x][j]); printf("\n"); for(int i=head[x];i<=tail[x];i++){int use=0;if(out[x][i])continue;string c=hand[x][i];if(c[0]=='P')if(P(x)){out[x][i]=1;use=1;}if(c[0]=='K'&&(!kill||nu[x]))if(K(x)){out[x][i]=1;kill=1;if(nu[x]==1)kill=0;use=1;}if(c[0]=='F')if(F(x)){out[x][i]=1;use=1;}if(c[0]=='N'){N(x);out[x][i]=1;use=1;}if(c[0]=='W'){W(x);out[x][i]=1;use=1;}if(c[0]=='Z'){nu[x]=1;out[x][i]=1;use=1;}if(dead[x])break;if(blood[1]==0)return 0;if(sum==0)return 1;printf("%c\n",c[0]);for(int k=1;k<=n;k++) {printf("pig%d %s jump%d blood%d ",k,pig[k],jump[k],blood[k]);for(int j=head[k];j<=tail[k];j++)if(!out[k][j])printf("%s ",hand[k][j]);printf("\n");}system("pause");if(use==1)i=head[x]-1,printf("%d will check again\n",x);}while(out[x][head[x]]==1)head[x]++;x=nex[x];}
}
void init()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%s",pig[i]);if(pig[i][0]=='F')sum++;for(int j=1;j<=4;j++)scanf("%s",hand[i][j]);tail[i]=4;head[i]=1;fro[i]=i-1;nex[i]=i+1;blood[i]=4;}fro[1]=n;nex[n]=1;jump[1]=1;for(int i=1;i<=m;i++)scanf("%s",card[i]);
}
void jieshu()
{if(play())printf("MP\n");else printf("FP\n");for(int i=1;i<=n;i++){if(dead[i]){
printf("DEAD\n");continue;}int sum=0;char hh[2005][3];for(int j=head[i];j<=tail[i];j++)if(!out[i][j])hh[++sum][0]=hand[i][j][0];for(int j=1;j<sum;j++)printf("%c ",hh[j][0]);if(sum==0)printf("\n");else printf("%c\n",hh[sum][0]);}
}
int main()
{init();//预处理jieshu();
}