id=2996
POJ训练计划中的模拟都是非常棒的模拟,也非常有代表性。
这个题讲的是给你一个国际象棋棋盘,敲代码打印出黑白两方的棋子。以及棋子的坐标。
可是须要注意的国际棋盘的坐标问题
例如以下图这个国际棋盘
能够看到数字轴和字母轴的方向以及增减关系。
所以在这个题的统计的时候须要进行坐标转换。由于已经做过类似的方法了,这个也不是问题。
总之就是个模拟,代码例如以下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> struct node { char x,y; }zb[10000]; char MAP[20][40]; int cmp (const void * a,const void * b) { struct node *ta = (struct node *)a; struct node *tb = (struct node *)b; if (ta->y == tb->y) return ta->x - tb->x; return ta->y - tb->y; } int cmp1 (const void * a,const void * b) { struct node *ta = (struct node *)a; struct node *tb = (struct node *)b; if (ta->y == tb->y) return ta->x - tb->x; return tb->y - ta->y; } int fin(char c) { int i,k; int s = 0; int x = 0,y = 0;; for (i = 1,y = 0;i < 17;y++,i += 2) { for (k = 2,x = 0;k < 33;x++,k += 4) if (MAP[i][k] == c) { zb[s].x = x; zb[s++].y = 8 - y; //printf ("!%d - %d! ",x,8 - y); } } return s; } int main() { int i,k; int n; for (i = 0;i < 17;i++) scanf ("%s",MAP[i]); printf ("White: "); n = fin('K'); qsort (zb,n,sizeof (zb[0]),cmp); for (i = 0;i < n;i++) printf ("K%c%d,",zb[i].x + 'a',zb[i].y); n = fin('Q'); qsort (zb,n,sizeof (zb[0]),cmp); for (i = 0;i < n;i++) printf ("Q%c%d,",zb[i].x + 'a',zb[i].y); n = fin('R'); qsort (zb,n,sizeof (zb[0]),cmp); for (i = 0;i < n;i++) printf ("R%c%d,",zb[i].x + 'a',zb[i].y); n = fin('B'); qsort (zb,n,sizeof (zb[0]),cmp); for (i = 0;i < n;i++) printf ("B%c%d,",zb[i].x + 'a',zb[i].y); n = fin('N'); qsort (zb,n,sizeof (zb[0]),cmp); for (i = 0;i < n;i++) printf ("N%c%d,",zb[i].x + 'a',zb[i].y); n = fin('P'); qsort (zb,n,sizeof (zb[0]),cmp); for (i = 0;i < n;i++) { printf ("%c%d",zb[i].x + 'a',zb[i].y); if (i < n - 1) printf(","); else printf (" "); } printf ("Black: "); n = fin('k'); qsort (zb,n,sizeof (zb[0]),cmp1); for (i = 0;i < n;i++) printf ("K%c%d,",zb[i].x + 'a',zb[i].y); n = fin('q'); qsort (zb,n,sizeof (zb[0]),cmp1); for (i = 0;i < n;i++) printf ("Q%c%d,",zb[i].x + 'a',zb[i].y); n = fin('r'); qsort (zb,n,sizeof (zb[0]),cmp1); for (i = 0;i < n;i++) printf ("R%c%d,",zb[i].x + 'a',zb[i].y); n = fin('b'); qsort (zb,n,sizeof (zb[0]),cmp1); for (i = 0;i < n;i++) printf ("B%c%d,",zb[i].x + 'a',zb[i].y); n = fin('n'); qsort (zb,n,sizeof (zb[0]),cmp1); for (i = 0;i < n;i++) printf ("N%c%d,",zb[i].x + 'a',zb[i].y); n = fin('p'); qsort (zb,n,sizeof (zb[0]),cmp1); for (i = 0;i < n;i++) { printf ("%c%d",zb[i].x + 'a',zb[i].y); if (i < n - 1) printf(","); else printf (" "); } return 0; }