| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
水题啊#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct place{
int x,y;
place(){x=y=0;}
place(int x,int y): x(x),y(y){}
};
ostream& operator<<(ostream& out, const place& p){
out << (char)('A'+p.x) << p.y+1;
return out;
}
bool operator<(const place& p1, const place& p2){
return p1.x<p2.x || (p1.x==p2.x && p1.y<p2.y);
}
char data[9][9], piece;
int dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
bool inRange(int x, int y){
return x>=0 && x<8 && y>=0 && y<8;
}
int getMoveSz(int i, int j, int d){
int res = 1;
for(int k = 1; ; k++){
if(!inRange(i+k*dir[d][0], j+k*dir[d][1])) break;
if(data[i+k*dir[d][0]][j+k*dir[d][1]] != '.') res++;
}
for(int k = 1; ; k++){
if(!inRange(i-k*dir[d][0], j-k*dir[d][1])) break;
if(data[i-k*dir[d][0]][j-k*dir[d][1]] != '.') res++;
}
return res;
}
int getMove(int i, int j){
if(data[i][j] != piece) {
//cout << "Not a moving piece for " << i << ", " << j << endl;
return 0;
}
place mover(i,j);
vector<place> movees;
for(int d = 0; d < 8; d++){
int moveSz = getMoveSz(i,j,d);
int dx = dir[d][0], dy = dir[d][1];
if(!inRange(i+moveSz*dx, j+moveSz*dy) || data[i+moveSz*dx][j+moveSz*dy] == piece) continue;
//cout << "move size for " << i << ", " << j << "on direction " << d << ": " << moveSz << endl;
bool ky = true;
for(int k = 1; k < moveSz; k++){
char c = data[i+k*dx][j+k*dy];
if(c != piece && c != '.'){
ky = false;
break;
}
}
if(ky) movees.push_back(place(i+moveSz*dx, j+moveSz*dy));
}
if(movees.empty()) return 0;
sort(movees.begin(), movees.end());
int sz = movees.size();
for(int k = 0; k < sz; k++) cout << mover << "-" << movees[k] << endl;
return sz;
}
int main() {
while(scanf("%s", data[0]) > 0){
for(int i = 1; i < 8; i++) scanf("%s", data[i]);
scanf("\n%c", &piece);
//cout << "piece: " << piece << endl;
int cnt = 0;
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
cnt += getMove(i,j);
}
}
if(!cnt) cout << "No moves are possible" << endl;
cout << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator