题解:
26个字母当成一个圆,最大距离不超过13
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int t;
int n;
char s[55];
int cas = 0;
scanf("%d",&t);
while (t--) {
cas++;
int min = 1000;
int count = 0;
scanf("%d",&n);
scanf("%s",s);
for(int i = 0;i <= n-4;i++){
count = 0;
//转化成A的情况
if(s[i] <= 'M')
count += abs(s[i]-'A');
else
count += abs('Z'-s[i]+1);
//转化成C的情况
if(s[i+1] <= 'O' && s[i+1] >= 'A')
count += abs(s[i+1]-'C');
else
count += abs('Z'-s[i+1]+3);
//转化成G的情况
if(s[i+2] <= 'G' && s[i+2] >= 'A')
count += abs(s[i+2]-'A'+7);
else
count += abs(s[i+2]-'T');
//转化成T的情况
if(s[i+3] <= 'S' && s[i+3] >= 'A')
count += abs(s[i+3]-'G');
else
count += abs('Z'-s[i+3]+7);
if(count == 0){
min = 0;break;
}
if(count < min)
min = count;
}
printf("Case #%d:\n%d\n",cas,min);
}
return 0;
}
题解:
考虑将被将军的情况:
1.棋子落在与将同一横轴的点上将军
2.棋子落在与将同一纵轴的点上将军
3.棋子与将位置呈“日”字
代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20;
char s[maxn][maxn];
int n;
inline bool judge(int x,int y,char c){
return x >= 1 && x <= 10 && y >= 1 && y <= 9 && s[x][y] == c;
}
int eatr(char c,int row,int col){
int num = 10;
for(int i = 1;i <= 9;i++){
if(s[row][i] == c){
num = 0;
for(int j = min(i,col)+1;j <= max(i,col)-1;j++){
if(s[row][j] != '.'){
num++;
}
}
}
}
return num;
}
int eatc(char c,int row,int col){
int num = 10;
for(int i = 1;i <= 10;i++){
if(s[i][col] == c){
num = 0;
for(int j = min(i,row)+1;j <= max(i,row)-1;j++){
if(s[j][col] != '.'){
num++;
}
}
}
}
return num;
}
int main(){
int m,cas = 0;
scanf("%d",&n);
//输入数据
while (n--) {
cas++;
scanf("%d",&m);
for(int i = 1;i <= 10;i++){
scanf("%s",s[i]+1);
}
printf("Case #%d:\n",cas);
bool ok = 0;
int colK = 0, rowK = 0;
int colG = 0, rowG = 0;
for(int i = 1;i <= 10;i++){
for(int j = 1;j <= 9;j++){
if(s[i][j] == 'K') {
rowK = i;
colK = j;
}
if(s[i][j] == 'G') {
rowG = i;
colG = j;
}
}
}
//炮吃将
if(eatr('C', rowK, colK) == 1)
ok = 1;
if(eatc('C', rowK, colK) == 1)
ok = 1;
//炮吃帅
if(eatr('c', rowG, colG) == 1)
ok = 1;
if(eatc('c', rowG, colG) == 1)
ok = 1;
//车吃将
if(eatr('R', rowK, colK) == 0)
ok = 1;
if(eatc('R', rowK, colK) == 0)
ok = 1;
//车吃帅
if(eatr('r', rowG, colG) == 0)
ok = 1;
if(eatc('r', rowG, colG) == 0)
ok = 1;
//兵吃将
if(judge(rowK+1, colK, 'P') || judge(rowK,colK+1,'P') || judge(rowK,colK-1,'P'))ok = 1;
//兵吃帅
if(judge(rowG-1, colG, 'p') || judge(rowG,colG+1,'p') || judge(rowG,colG-1,'p'))ok = 1;
//马吃帅
int tx,ty;
tx = rowG - 1; ty = colG - 2;
if(judge(tx,ty,'n')) {
if(s[tx][ty+1] == '.') ok = 1;
}
tx = rowG - 2; ty = colG - 1;
if(judge(tx,ty,'n')) {
if(s[tx+1][ty] == '.') ok = 1;
}
tx = rowG - 2; ty = colG + 1;
if(judge(tx,ty,'n')) {
if(s[tx+1][ty] == '.') ok = 1;
}
tx = rowG - 1; ty = colG + 2;
if(judge(tx,ty,'n')) {
if(s[tx][ty-1] == '.') ok = 1;
}
tx = rowG + 1; ty = colG - 2;
if(judge(tx,ty,'n')) {
if(s[tx][ty+1] == '.') ok = 1;
}
tx = rowG + 2; ty = colG - 1;
if(judge(tx,ty,'n')) {
if(s[tx-1][ty] == '.') ok = 1;
}
tx = rowG + 1; ty = colG + 2;
if(judge(tx,ty,'n') || judge(tx,ty,'N')) {
if(s[tx][ty-1] == '.') ok = 1;
}
tx = rowG + 2; ty = colG + 1;
if(judge(tx,ty,'n')) {
if(s[tx-1][ty] == '.') ok = 1;
}
//马吃将
tx = rowK - 1; ty = colK - 2;
if(judge(tx,ty,'N')) {
if(s[tx][ty+1] == '.') ok = 1;
}
tx = rowK - 2; ty = colK - 1;
if(judge(tx,ty,'N')) {
if(s[tx+1][ty] == '.') ok = 1;
}
tx = rowK - 2; ty = colK + 1;
if(judge(tx,ty,'N')) {
if(s[tx+1][ty] == '.') ok = 1;
}
tx = rowK - 1; ty = colK + 2;
if(judge(tx,ty,'N')) {
if(s[tx][ty-1] == '.') ok = 1;
}
tx = rowK + 1; ty = colK - 2;
if(judge(tx,ty,'N')) {
if(s[tx][ty+1] == '.') ok = 1;
}
tx = rowK + 2; ty = colK - 1;
if(judge(tx,ty,'N')) {
if(s[tx-1][ty] == '.') ok = 1;
}
tx = rowK + 1; ty = colK + 2;
if(judge(tx,ty,'N')) {
if(s[tx][ty-1] == '.') ok = 1;
}
tx = rowK + 2; ty = colK + 1;
if(judge(tx,ty,'N')) {
if(s[tx-1][ty] == '.') ok = 1;
}
if(!ok) printf("No\n");
else printf("Yes\n");
}
}