HDU 1107 武林

/************************************************************
 * @Nstd
 * 题号:HDU - 1107
 * 类型:模拟
 * 题目:在一个12*12的棋盘上有三个门派进行决斗
 *           三个门派的人分别往固定的三个方向走
 *           走到底再往反向
 *           当某格上只有不同门派的两个人时,才能进行决斗
 *           所有的人都走完一步后才开始计算要不要决斗
 *           扣的血按公式计算
 *           初始状态算第一次决斗
 * 思路:开个结构体存人的基本信息(包括走向)
 *           开个棋盘结构体,存当前有几个人和一个链表头
 *           开一个链表,存棋盘(x,y)所在的人的派别和编号
 *           棋盘结构体的链表头指向那个链表
 *           判断的时候先找棋盘中能打架的人,然后进行伤害计算
 *           走路的时候同时跟新棋盘和链表
 * 问题:思路不清晰,从中午开始做的,断断续续做到了晚上12点
 *           有两个地方的"|"被我写成了"&",ubuntu下用vim不会调试
 *           用的printf()进行调试,结果调了老半天
 * 特例:无
 *
 *                                    —— 2012/3/19
 ************************************************************/

hdu - 1107
  1 #include <stdio.h>
2 #include <iostream>
3 #include <string.h>
4 #include <algorithm>
5 using namespace std;
6
7 #define N 1010
8 #define cls(a) memset(a, 0, sizeof(a))
9
10 //0少林,1武当,2峨嵋
11 struct SB{
12 int x, y; //pepole的当前坐标
13 int dx, dy; //行走方向
14 int hp, sp, atk; //血量、内力、攻击力
15 bool isDead; //是否死亡
16 }pep[3][N];
17
18 struct nd{
19 int next; //链表指针
20 int id; //0-9位存编号,10-19位存派别
21 }node[N];
22
23 struct Land{
24 int all; //当前格内的总人数
25 int state; //0-9,10-19,20-29分别存当前格内三个派别的人数
26 int next; //指向链表头,用头插入法进行插入
27 }land[13][13];
28
29 int table[256]; //根据字母得到派别
30 int cntn, n, x, y, hp, sp, atk, step, ct[3];
31 int dir[3][2] = {{1,0},{0,1},{1,1}}, pc[3]={0,10,20};
32 double tag[3][2] = {{0.5,0.5},{0.8,0.2},{0.2,0.8}};
33
34 void set(struct SB a[], int &d, int move, int k)
35 {
36 a[d].x = x; a[d].y = y;
37 a[d].dx = dir[k][0];
38 a[d].dy = dir[k][1];
39 a[d].hp = hp; a[d].sp = sp;
40 a[d].atk = atk; a[d].isDead = 0;
41 land[x][y].all++; //统计总人数
42 land[x][y].state += (1<<move); //0-9,10-19,20-29位分别记录s,w,e的人数
43 node[++cntn].next = land[x][y].next;//建立一个链表用来存当前格有谁
44 land[x][y].next = cntn;
45 node[cntn].id = (k<<10) | d++; //0-9存下标,10-19存帮派
46 }
47
48 int attak(int d, int id)
49 {
50 double dhp=0;
51 dhp = (tag[d][0]*pep[d][id].sp + tag[d][1]*pep[d][id].atk) *
52 (pep[d][id].hp + 10) / 100;
53 return (int)dhp;
54 }
55
56 void check()
57 {
58 int i, j;
59 int ida, idb, mpa, mpb, da, db, hpa, hpb;
60 for(i=1; i<13; i++)
61 {
62 for(j=1; j<13; j++)
63 {
64 //一开始,下面的"|"被我写成"&"了
65 if(land[i][j].all == 2 &&
66 (((land[i][j].state>>10) |
67 (land[i][j].state>>20) |
68 land[i][j].state) & 0x2ff)==1 )
69 {
70 ida = land[i][j].next;
71 idb = node[ida].next;
72 mpa = node[ida].id>>10;
73 da = node[ida].id & 0x02ff;
74 mpb = node[idb].id>>10;
75 db = node[idb].id & 0x02ff;
76 if(!pep[mpa][da].isDead && !pep[mpb][db].isDead)
77 {
78 hpa = attak(mpa, da);
79 hpb = attak(mpb, db);
80 pep[mpa][da].hp -= hpb;
81 pep[mpb][db].hp -= hpa;
82 if(pep[mpa][da].hp <= 0) pep[mpa][da].isDead = 1;
83 if(pep[mpb][db].hp <= 0) pep[mpb][db].isDead = 1;
84 }
85 }
86 }
87 }
88 }
89
90 void walk()
91 {
92 int i, k, xx, yy;
93 cntn = 0; cls(land);
94 for(k=0; k<3; k++)
95 {
96 for(i=0; i<ct[k]; i++)
97 {
98 if(!pep[k][i].isDead)
99 {
100 xx = pep[k][i].x + pep[k][i].dx;
101 yy = pep[k][i].y + pep[k][i].dy;
102 if(xx < 1 || xx > 12 || yy < 1 || yy > 12)
103 {
104 pep[k][i].dx = -pep[k][i].dx;
105 pep[k][i].dy = -pep[k][i].dy;
106 xx += 2*pep[k][i].dx;
107 yy += 2*pep[k][i].dy;
108 if(xx > 0 && xx < 13 && yy > 0 && yy < 13)
109 {
110 pep[k][i].x = xx;
111 pep[k][i].y = yy;
112 }
113 else
114 {
115 xx -= pep[k][i].dx;
116 yy -= pep[k][i].dy;
117 }
118 }
119 else
120 {
121 pep[k][i].x = xx;
122 pep[k][i].y = yy;
123 }
124 land[xx][yy].all++;
125 land[xx][yy].state += (1<<pc[k]);
126 node[++cntn].next = land[xx][yy].next;
127 land[xx][yy].next = cntn;
128 node[cntn].id = (k<<10) | i; //这里和下面的"|"也写成"&"了
129 }
130 }
131 }
132 }
133
134 int main()
135 {
136 int i, j;
137 char ch[2];
138 table['S'] = 0;
139 table['W'] = 1;
140 table['E'] = 2;
141
142 scanf("%d", &n);
143 while(n--)
144 {
145 cntn = 0;
146 ct[0] = ct[1] = ct[2] = 0;
147 scanf("%d", &step);
148 cls(land); cls(node);
149 while(scanf("%s", ch)!=EOF && ch[0]>='A')
150 {
151 scanf("%d%d%d%d%d", &x, &y, &sp, &atk, &hp);
152 i = table[ch[0]];
153 set(pep[i], ct[i], i*10, i);
154 }
155 for(i=0; i<step; i++)
156 {
157 check();
158 walk();
159 }
160 int nhp[3]={0}, sum[3]={0};
161 for(i=0; i<3; i++)
162 {
163 for(j=0; j<ct[i]; j++)
164 {
165 if(!pep[i][j].isDead)
166 {
167 nhp[i] += pep[i][j].hp;
168 sum[i]++;
169 }
170 }
171 }
172 for(i=0; i<3; i++)
173 {
174 printf("%d %d\n", sum[i], nhp[i]);
175 }
176 printf("***\n");
177 }
178 return 0;
179 }



原文地址:https://www.cnblogs.com/Nstd/p/2407598.html