战棋游戏

一道繁琐的模拟题(虽然要带spfa) 调试好久才过

注意这道题的几个细节

1 》经过敌方军营就不能入队

2》己方军队不能打己方人

3》一个军队死了就将其从图中移除

4》另外程序码量大不要打错变量了(orz)

自打了3000多码 程序有点乱

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #define mod 100000
  6 using namespace std;
  7 int n,m,c,e,i,j,o1,o2,now,p,l,w,h,t;
  8 int a[110][110],q[101000][3],g[110][110],dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};
  9 struct pe{int hp,at,mv,ad1,ad2,stx,sty,gr;}role[110];
 10 char s[100];
 11 int b[110][110],bo[110][110],die[110];
 12 void init()
 13 {
 14     int tap=0;
 15     for(i=1;i<l;++i)
 16     if(s[i]<58&s[i]>47)
 17     {
 18         int q=0;
 19         while(s[i]<58&s[i]>47)
 20         {
 21             q=q*10+s[i]-48;
 22             i++;
 23         }
 24         o1=q;
 25         i++;q=0;
 26         if(i>=l)break;
 27         if(s[i]<58&s[i]>47)
 28         {
 29             while(s[i]<58&s[i]>47)
 30             {
 31                 q=q*10+s[i]-48;
 32                 i++;
 33             }
 34             o2=q;
 35         }
 36         break;
 37     }
 38 }
 39 bool mo(int x,int y)
 40 {
 41     if(role[p].stx==x&&role[p].sty==y)return true;
 42     memset(b,0x7f,sizeof(b));
 43     memset(bo,0,sizeof(bo));
 44     b[role[p].stx][role[p].sty]=0;
 45     int tx,ty;
 46     h=t=0;
 47     ++t;
 48     q[t][1]=role[p].stx;
 49     q[t][2]=role[p].sty;
 50     a[role[p].stx][role[p].sty]=0;
 51     while(h!=t)
 52     {
 53         int now;
 54         h%=mod;h++;bo[q[h][1]][q[h][2]]=0;
 55         for(int z=0;z<4;++z)
 56         {
 57             tx=q[h][1]+dx[z];
 58             ty=q[h][2]+dy[z];
 59             now=b[q[h][1]][q[h][2]];
 60             if(a[tx][ty]||now+g[tx][ty]>role[p].mv)continue;
 61             if(b[tx][ty]<now+g[tx][ty]||tx<1||ty<1||tx>n||ty>m)continue;
 62             b[tx][ty]=now+g[tx][ty];
 63             for(j=0;j<4;++j)
 64             if(a[tx+dx[j]][ty+dy[j]]>0&&role[a[tx+dx[j]][ty+dy[j]]].gr!=role[p].gr)
 65                 b[tx][ty]=role[p].mv;
 66             if(bo[tx][ty])continue;
 67             t%=mod;t++;q[t][1]=tx;q[t][2]=ty;bo[tx][ty]=1;
 68         }
 69     }
 70     if(b[x][y]<0x7f7f7f7f)
 71     {
 72         role[p].mv-=b[x][y];
 73         for(j=0;j<4;++j)
 74         if(a[x+dx[j]][y+dy[j]]>0&&role[a[x+dx[j]][y+dy[j]]].gr!=role[p].gr)
 75         role[p].mv=0;
 76         a[x][y]=p;
 77         role[p].stx=x;
 78         role[p].sty=y;
 79         return true;
 80     }
 81     a[role[p].stx][role[p].sty]=1;
 82     return false;
 83 }
 84 int abss(int x){return x<0? -x:x;};
 85 bool cd(int x)
 86 {
 87     int w=abss(role[p].stx-role[x].stx)+abss(role[p].sty-role[x].sty);
 88     if(w<role[p].ad1|w>role[p].ad2)return false;
 89     return true;
 90 }
 91 bool atc(int x)
 92 {
 93     role[x].hp-=role[p].at;
 94     if(role[x].hp <=0)return true;
 95     else return false;
 96 }
 97 void work()
 98 {
 99     o1,o2;
100     init();
101     if(s[0]=='R')
102     {now=o1;return;}
103     if(s[0]=='A'&s[1]=='c')
104     {p=o1;return;}
105     if(s[0]=='M')
106     {
107         int u=role[p].mv;
108         if(mo(o1,o2))printf("%d\n",role[p].mv);
109         else printf("INVALID\n");
110         role[p].mv=u;
111     }
112     else if(s[0]=='D')
113     {
114         if(die[o1]||!cd(o1)||role[p].gr==role[o1].gr)
115         {printf("INVALID\n");return;}
116         if(atc(o1))
117         {
118             printf("%d\n",role[o1].hp);die[o1]=1;
119             a[role[o1].stx][role[o1].sty]=0;
120         }
121         else printf("INVALID\n"),role[o1].hp+=role[p].at;
122     }
123     else 
124     {    
125         if(die[o1]||!cd(o1)||role[p].gr==role[o1].gr)
126         {printf("INVALID\n");return;}
127         if(!atc(o1))printf("%d\n",role[o1].hp);
128         else printf("INVALID\n"),role[o1].hp+=role[p].at;
129     }
130 }
131 int main()
132 {
133     //freopen("xx.in","r",stdin);
134     //freopen("xx.out","w",stdout);
135     scanf("%d%d%d%d",&n,&m,&c,&e);
136     for(i=1;i<=n;++i)
137     for(j=1;j<=m;++j)
138     scanf("%d",&g[i][j]);
139     for(i=1;i<=c;++i)
140     scanf("%d%d%d%d%d%d%d%d",&role[i].hp,&role[i].at,&role[i].mv,&role[i].ad1,&role[i].ad2,&role[i].stx,&role[i].sty,&role[i].gr);
141     scanf("\n");
142     for(i=1;i<=c;++i)
143     a[role[i].stx][role[i].sty]=i;
144     for(;e--;)
145     {
146         l=strlen(gets(s));
147         if(!l)break;
148         work();
149     }
150 }

 

posted @ 2016-10-28 19:46  peter863  阅读(246)  评论(0编辑  收藏  举报