码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-2996 Help Me with the Game---模拟棋子

时间:2018-04-04 23:17:36      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:重要   def   har   tar   com   传送门   cout   注意   int   

题目链接:

https://vjudge.net/problem/POJ-2996

题目大意:

给出白方和黑方的棋子和对应的坐标,输出该副棋盘的样子

1,棋盘中大写字母表示的是白方棋子,小写是黑方。
2,注意棋盘的行数是从最下面开始计数的。和数组的下标相反。也就是说数组行数为8的棋盘行   数为1(数组从1开始)。一开始就写错了
3,最容易忽略也最重要的是:白棋和黑棋在输出的时候其实排序规则是不一样的,白棋先是行号从小到大,同一行列号从小到大,黑棋先是行号从大到小,同一行列号从小到大(但是棋子的类型都要按照KQRBNP顺序)。

模拟最重要的就是细节

用了一些操作让代码没那么繁琐。

还有一题输入和输出正好相反:传送门

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<queue>
  7 #include<stack>
  8 #include<map>
  9 using namespace std;
 10 typedef long long ll;
 11 const int maxn = 1e2 + 10;
 12 const int INF = 1 << 30;
 13 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
 14 int T, n, m, x;
 15 char Map[40][40];
 16 map<char, vector<string> >M;
 17 char a[] = "kqrbn";
 18 char b[] = "KQRBN";
 19 int main()
 20 {
 21     for(int i = 0; i < 17; i++)
 22         for(int j = 0; j < 33; j++)cin >> Map[i][j];
 23     vector<string>ans1, ans2;
 24     for(int i = 1; i <= 15; i += 2)//从上往下扫描黑棋(小写字母)
 25     {
 26         for(int j = 2; j <= 30 ; j += 4)
 27         {
 28             string s;//位置字符串
 29             s += (char)(j / 4 + a);//列号
 30             s += (char)((17 - i) / 2 + 0);//行号 !!注意,这里最下面是编号第一行
 31             //cout<<s<<endl;
 32             if(Map[i][j] == p)ans1.push_back(s);
 33             for(int k = 0; k < 5; k++)
 34             {
 35                 if(Map[i][j] == a[k])
 36                 {
 37                     M[a[k]].push_back(s);
 38                 }
 39             }
 40         }
 41     }
 42     for(int i = 15; i >= 1; i -= 2)//从下往上扫描白棋(大写字母)
 43     {
 44         for(int j = 2; j <= 30; j += 4)
 45         {
 46             string s;//位置字符串
 47             s += (char)(j / 4 + a);//列号
 48             s += (char)((17 - i) / 2 + 0);//行号 !!注意,这里最下面是编号第一行
 49             //cout<<s<<endl;
 50             if(Map[i][j] == P)ans2.push_back(s);
 51             for(int k = 0; k < 5; k++)
 52             {
 53                 if(Map[i][j] == b[k])
 54                 {
 55                     M[b[k]].push_back(s);
 56                 }
 57             }
 58         }
 59     }
 60     printf("White: ");
 61     int tot = 0, c = 1;
 62     for(int i = 0; i < 5; i++)tot += M[b[i]].size();
 63     tot += ans2.size();
 64     for(int i = 0; i < 5; i++)
 65     {
 66         for(int j = 0; j < M[b[i]].size(); j++)
 67         {
 68             cout<<b[i]<<M[b[i]][j];
 69             if(c != tot)
 70             {
 71                 c++;
 72                 cout<<",";
 73             }
 74         }
 75     }
 76     for(int i = 0; i < ans2.size(); i++)
 77     {
 78         cout<<ans2[i];
 79         if(c != tot)
 80         {
 81             c++;
 82             cout<<",";
 83         }
 84     }
 85     cout<<endl;
 86     printf("Black: ");
 87     tot = 0, c = 1;
 88     for(int i = 0; i < 5; i++)tot += M[a[i]].size();
 89     tot += ans1.size();
 90     for(int i = 0; i < 5; i++)
 91     {
 92         for(int j = 0; j < M[a[i]].size(); j++)
 93         {
 94             cout<<(char)(a[i] - 32)<<M[a[i]][j];
 95             if(c != tot)
 96             {
 97                 c++;
 98                 cout<<",";
 99             }
100         }
101     }
102     for(int i = 0; i < ans1.size(); i++)
103     {
104         cout<<ans1[i];
105         if(c != tot)
106         {
107             c++;
108             cout<<",";
109         }
110     }
111     cout<<endl;
112     return 0;
113 }

 

POJ-2996 Help Me with the Game---模拟棋子

标签:重要   def   har   tar   com   传送门   cout   注意   int   

原文地址:https://www.cnblogs.com/fzl194/p/8719061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!