象棋该怎么做

1. 象棋

XJOI - 题目ID:9676必做题100分

最新提交:0 分

历史最高:0 分

时间限制: 500ms

空间限制: 65536kB

题目描述

【题目描述】

马走日,象走田,这是中国象棋的规则。鱼大大非常喜欢下中国象棋,也十分精通中国象棋,象棋中每个棋子都有自己对应的价值,如:车9分,马炮4.5分,士象3分,兵1分,过河兵2分。但是他认为象棋中有一个非常bug的点,就是马居然比象更厉害,价值更高?!以象命名的象棋中的象居然过的这么惨,这不科学!于是他自己设定了一个修改版的象棋,增强了“象”,使其比“马”更加灵活厉害。
修改后。每个子的价值如下:将帅0分(将帅要是都没了,还算啥分呀),车9分,炮8分,象7分,马4分,士3分,兵1分,过河兵2分。其中,象可以走4格直线或者走目字走田字,并且无象脚(不会被其他子阻挡)还可过河,其余棋子移动规则不变。终于!象被无限加强了,是时候向马儿复仇了!现给出一残局,在马不动的情况下,问象最少几步可以吃掉马?如果吃不到马则输出“No!God!Please no!”。

说明:象马出现位置随机,保证残局有且只有一象一马,象可以吃掉任意棋子(即若象移动后的落点位有棋子,会直接吃掉并落点)
注:象走说明:以下图为例,此时第5行第3列的黑象有13个落点位在棋盘内,可直接走过去,还有3个落点位在棋盘外,不可走。

输入格式

【输入格式】
一个10*9的二维数组,表示象棋棋盘,其中,数字对应各棋子的分数,0代表该位置空或为将帅

输出格式

【输出格式】
象吃掉马时的步数

样例

Input 1

0 0 0 0 8 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 9 0 0 0 0 8 7 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0

Output 1

No!God!Please no!

Input 2

0 0 0 0 8 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 8 7 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0

Output 2

No!God!Please no!

哪位大佬能告诉我该怎么做 :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob:

蒟蒻代码

#include <bits/stdc++.h>
#include <queue>
using namespace std;
const int dx[] = {-2, -2, 2, 2, -1, -1, 1, 1};
const int dy[] = {-1, 1, -1, 1, -2, 2, -2, 2};

struct Point {
    int x, y;
};

bool isValid(int x, int y) {
    return x >= 0 && x < 10 && y >= 0 && y < 9;
}

int main() {
    int board[10][9];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 9; j++) {
            cin >> board[i][j];
        }
    }

    Point horse;
    Point elephant;

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 9; j++) {
            if (board[i][j] == 4) {
                horse.x = i;
                horse.y = j;
            }
            if (board[i][j] == 7) {
                elephant.x = i;
                elephant.y = j;
            }
        }
    }

    queue<pair<Point, int>> q;
    q.push(make_pair(elephant, 0));
    bool visited[10][9] = {false};
    visited[elephant.x][elephant.y] = true;

    while (!q.empty()) {
        Point curr = q.front().first;
        int steps = q.front().second;
        q.pop();

        if (curr.x == horse.x && curr.y == horse.y) {
            cout << steps << endl;
            return 0;
        }

        for (int i = 0; i < 8; i++) {
            int nx = curr.x + dx[i];
            int ny = curr.y + dy[i];

            if (isValid(nx, ny) && !visited[nx][ny]) {
                visited[nx][ny] = true;
                q.push(make_pair(Point{nx, ny}, steps + 1));
            }
        }
    }

    cout << "No!God!Please no!" << endl;

    return 0;
}

给解决方案谢谢啦~

eee…只有10分

能这么写吗? :thinking:

const去掉试试?

理论上来讲没问题

给你个帖子吧 有讲题解的

我真的是蒟蒻

#include<bits/stdc++.h>
using namespace std;
int dx[ ]={-4,-3,-3,-2,-2,-1,-1,0,0,1,1,2,2,3,3,4};
int dy[ ]={0,1,-1,2,-2,3,-3,4,-4,3,-3,2,-2,1,-1,0};
int n,ans,vis[20][20],A,B,C,D,a[20][20];
struct node{
	int x,y,step;
};
queue<node>que;
void bfs(int x,int y){
	vis[x][y]=1;
	que.push({x,y,0});
	while(que.size()){
        node hd=que.front();
        que.pop();
        if(hd.x==C && hd.y==D){
            cout<<hd.step;
            return ;
        }
        for(int i=0;i<16;i++){
            int nx=hd.x+dx[i];
            int ny=hd.y+dy[i];
            int nstep=hd.step+1;
            if((nx<1 || ny<1 || nx>10 || ny>9 || vis[nx][ny] || a[nx][ny]!=0) && a[nx][ny]!=4){
                continue;
            }
            vis[nx][ny]=1;
            que.push({nx,ny,nstep});
        }
    }
    cout << "No!God!Please no!";
}
int main(){
    for(int i=1;i<=10;i++){
    	for(int j=1;j<=9;j++){
    		cin>>a[i][j];
    		if(a[i][j]==7){
    			A=i;
    			B=j;
			}
			if(a[i][j]==4){
				C=i;
				D=j;
			}
		}
	}
	bfs(A,B);
    return 0;
}
1 个赞
#include<bits/stdc++.h>
using namespace std;
int dx[16] = {-4, -3, -3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4};
int dy[16] = {0, 1, -1, 2, -2, 3, -3, 4, -4, 3, -3, 2, -2, 1, -1, 0};
int n,s[305][305];
struct node{
	int x,y,p;
};
int a[20][20];
queue<node> que;
int A,B,C,D;
void bfs(int x,int y);
int main(){
    for(int i=1;i<=10;i++){
    	for(int j=1;j<=9;j++){
    		cin>>a[i][j];
    		if(a[i][j]==7){
    			A=i;
    			B=j;
			}
			if(a[i][j]==4){
				C=i;
				D=j;
			}
		}
	}
	bfs(A,B);
    return 0;
}
void bfs(int x,int y){
	s[A][B]=1;
	que.push({A,B,0});
	while(que.size()){
        node hd=que.front();
        que.pop();
        if(hd.x==C && hd.y==D){
            cout<<hd.p;
            return ;
        }
        for(int i=0;i<16;i++){
            int r=hd.x+dx[i];
            int c=hd.y+dy[i];
            int p=hd.p+1;
            if(s[r][c] || r<1 || r>10 || c<1 || c>9 || a[r][c]!=0&&a[r][c]!=4){
                continue;
            }
            s[r][c]=1;
            que.push({r,c,p});
        }
    }
    cout << "No!God!Please no!";
}