HDU 4121 Xiangqi 模拟题

题目: http://acm.hdu.edu.cn/showproblem.php?pid=4121

首先对标题赞一个,非要叫 “Xiangqi” 而不是 ”中国象棋“ 或者 ”Chinese chess“ 。。

然后是题意:黑棋只剩下一个”将“了,红棋各种 ”车” “马” “炮“,判断黑棋是不是死棋了。。。

对于一个会下中国象棋的选手简直简单啊,第一次手贱加脑残WA一次,稍微一改就0ms AC了,模拟题没什么可以讲的,代码中有详细注释。

  1 #include <stdio.h>

  2 #include <string.h>

  3 #include <algorithm>

  4 

  5 char palace[11][10]; //棋盘

  6 

  7 //判断(x, y)点有没有棋子

  8 char has_chess(int x, int y)

  9 {

 10     if(x < 1 || x > 10 || y < 1 || y > 9)

 11         return 0;

 12     return palace[x][y];

 13 }

 14 

 15 //判断(x, y)点是不是棋子c

 16 bool is_chess(int x, int y, char c)

 17 {

 18     return has_chess(x, y) == c;

 19 }

 20 

 21 //判断(x, y)点的左边直线上有没有棋子c,有的话返回横坐标

 22 int has_left(int x, int y, char c)

 23 {

 24     for(int i = 1; i < y; i++)

 25     {

 26         if(palace[x][i] == c)

 27             return i;

 28     }

 29     return 0;

 30 }

 31 

 32 //判断(x, y)点的右边直线上有没有棋子c,有的话返回横坐标

 33 int has_right(int x, int y, char c)

 34 {

 35     for(int i = 9; i > y; i--)

 36     {

 37         if(palace[x][i] == c)

 38             return i;

 39     }

 40     return 0;

 41 }

 42 

 43 //判断(x, y)点的上边直线上有没有棋子c,有的话返回纵坐标

 44 int has_up(int x, int y, char c)

 45 {

 46     for(int i = 1; i < x; i++)

 47     {

 48         if(palace[i][y] == c)

 49             return i;

 50     }

 51     return 0;

 52 }

 53 

 54 //判断(x, y)点的下边直线上有没有棋子c,有的话返回纵坐标

 55 int has_down(int x, int y, char c)

 56 {

 57     for(int i = 10; i > x; i--)

 58     {

 59         if(palace[i][y] == c)

 60             return i;

 61     }

 62     return 0;

 63 }

 64 

 65 //判断(x1, y)到(x2, y)之间棋子的数量

 66 int cnt_chess_x(int x1, int x2, int y)

 67 {

 68     if(x1 > x2)

 69         std::swap(x1, x2);

 70     int cnt = 0;

 71     for(x1++; x1 < x2; x1++)

 72     {

 73         if(palace[x1][y])

 74             cnt++;

 75     }

 76     return cnt;

 77 }

 78 

 79 //判断(x, y1)到(x, y2)之间棋子的数量

 80 int cnt_chess_y(int y1, int y2, int x)

 81 {

 82     if(y1 > y2)

 83         std::swap(y1, y2);

 84     int cnt = 0;

 85     for(y1++; y1 < y2; y1++)

 86     {

 87         if(palace[x][y1])

 88             cnt++;

 89     }

 90     return cnt;

 91 }

 92 

 93 //判断能否被“帅”吃掉

 94 bool general(int x, int y)

 95 {

 96     int gx, gy;

 97     for(int i = 8; i <= 10; i++)

 98         for(int j = 4; j <= 6; j++)

 99             if(palace[i][j] == 'G')

100             {

101                 gx = i;

102                 gy = j;

103             }

104     if(gy != y)return 0;

105     return cnt_chess_x(gx, x, y) == 0;

106 }

107 

108 //判断能否被“车”吃掉

109 bool chariot(int x, int y)

110 {

111     int tmp = has_left(x, y, 'R');

112     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0)

113         return 1;

114     tmp = has_right(x, y, 'R');

115     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0)

116         return 1;

117     tmp = has_up(x, y, 'R');

118     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0)

119         return 1;

120     tmp = has_down(x, y, 'R');

121     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0)

122         return 1;

123     return 0;

124 }

125 

126 //判断能否被“马”吃掉

127 bool horse(int x, int y)

128 {

129     return(is_chess(x-2, y-1, 'H') && !has_chess(x-1, y-1)

130         || is_chess(x-2, y+1, 'H') && !has_chess(x-1, y+1)

131         || is_chess(x+2, y-1, 'H') && !has_chess(x+1, y-1)

132         || is_chess(x+2, y+1, 'H') && !has_chess(x+1, y+1)

133         || is_chess(x-1, y-2, 'H') && !has_chess(x-1, y-1)

134         || is_chess(x-1, y+2, 'H') && !has_chess(x-1, y+1)

135         || is_chess(x+1, y-2, 'H') && !has_chess(x+1, y-1)

136         || is_chess(x+1, y+2, 'H') && !has_chess(x-1, y+1));

137 }

138 

139 //判断能否被“炮”吃掉

140 bool cannon(int x, int y)

141 {

142     int tmp = has_left(x, y, 'C');

143     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1)

144         return 1;

145     tmp = has_right(x, y, 'C');

146     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1)

147         return 1;

148     tmp = has_up(x, y, 'C');

149     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1)

150         return 1;

151     tmp = has_down(x, y, 'C');

152     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1)

153         return 1;

154     return 0;

155 }

156 

157 //判断是不是死棋

158 bool is_die(int x, int y)

159 {

160     if(x < 1 || x > 3 || y < 4 || y > 6)

161         return 1;

162     return (general(x, y) || chariot(x, y) || horse(x, y) || cannon(x, y));

163 }

164 

165 int main()

166 {

167     int n, xt, yt;

168     int x, y;

169     char s[2];

170     while(scanf("%d %d %d", &n, &xt, &yt) != EOF && (n || xt || yt))

171     {

172         memset(palace, 0, sizeof(palace));

173         for(int i = 0; i < n; i++)

174         {

175             scanf("%s %d %d", s, &x, &y);

176             palace[x][y] = s[0];

177         }

178 

179         //如果“将”向周围走一步不死或者在原地不死,就说明不是死棋

180         //(话说不知道该不该判断原地,因为我没读懂题,不知道轮到谁走棋了。。。)

181         if(!is_die(xt, yt) || !is_die(xt+1, yt) || !is_die(xt-1, yt) ||

182             !is_die(xt, yt+1) || !is_die(xt, yt-1))

183             printf("NO\n");

184         else

185             printf("YES\n");

186     }

187     return 0;

188 }
View Code

 

你可能感兴趣的:(HDU)