Shortest Path && White Knight's Movement (BFS)

breadth first search

insert image description here
insert image description here
*Image credit: Challenge Programming Competition (Version 2)

shortest path

insert image description here
insert image description here
insert image description here
insert image description here

insert image description here

http://m.blog.csdn.net/article/details?id=50768661。

#include <iostream>
#include <queue>
using namespace std;
const int MAX_N = 100;
const int MAX_M = 100;
const int INF = 0x3f3f3f3f;   //用来表示无穷大
typedef pair<int, int> P;   //定义一个坐标形式
char maze[MAX_N][MAX_M + 1];
int N, M;
int sx, sy; //起点的位置
int gx, gy; //终点的位置
 
int d[MAX_N][MAX_M];//储存起点到某一点的距离
int dx[4] = {
    
     1,0,-1,0 }, dy[4] = {
    
     0,1,0,-1 };  //移动方向
 
void bfs()
{
    
    
	queue<P> que;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			d[i][j] = INF;	     //将所有的点置空
	que.push(P(sx, sy));  //输入起点
	d[sx][sy] = 0;	    
 
	while (que.size())   //
	{
    
    
		P p = que.front(); que.pop(); //每一队都是一层(累积的距离相等),且只考虑一队
		int i;
		for (i = 0; i < 4; i++)		//输入目前这个点周围四个中没有被访问过的点
		{
    
    
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if (0 <= nx&&nx < N
				&& 0 <= ny&&ny < M
				&&maze[nx][ny] != '#'
				&&d[nx][ny] == INF)
		/*如果这个点(那四个点之一)
		1.在这个地图内
		2.这个点不是‘#’(不是墙)
		3.其值等于无穷大(未被访问过)
		*/
			{
    
    
				que.push(P(nx, ny));//满足以上条件即可进入队列
				d[nx][ny] = d[p.first][p.second] + 1; //目标位置下移了一次
				if(nx==gx && ny==gy) break;  //到了终点
 
                        }
		}
		if(i!=4) break;
	}
 
}
 
int main()
{
    
    
	cin>>N>>M;
	for (int i = 0; i < N; i++)
		cin>>maze[i];
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
		{
    
    
			if (maze[i][j] == 'S')
			{
    
    
				sx = i; sy = j;
			}
			if (maze[i][j] == 'G')
			{
    
    
				gx = i; gy = j;
			}
		}
	bfs();
	cout<<d[gx][gy]<<endl;
 
	return 0;
}

7-3 White Knight's move (20 points)

Little S first came into contact with chess. He found that the movement of Knight pieces in chess is similar to that of horses in Chinese chess, as shown in the figure.QQ picture 20191115182554.png

So little S randomly placed some chess pieces on the board, including a white knight, a black queen, several black chariots and some black bishops.

Little S wants to know how to eat the black queen with fewer steps while avoiding the attack range of the black chariot and the black bishop.

Note 1: The attack range of the chariot is a straight line, similar to that of a Chinese chess rook; the attack range of a bishop is a diagonal line, which can be extended infinitely without obstacles.

Note 2: The white knight can only eat the black queen, not the black chariot and black bishop.

Input format:
The input consists of only one set of examples.

A set of samples contains 8 lines (corresponding to lines 1-8 respectively), each line contains 8 characters, and each character represents the checkerboard condition of the corresponding row and column.

Among them, ' . ' means that there is no chess piece on the grid; ' K ' means that the white knight is placed on the grid; ' Q ' means that the black queen is placed on the grid; ' R ' means that the black queen is placed on the grid. chariot; 'B' means the black bishop is placed on the grid.

Note: The title ensures that the initial position of the white knight is not within the attack range of the black chariot and black bishop.

Output format:
If the white knight can eat the black queen while avoiding the attacks of the black chariot and the black bishop, then output the minimum number of steps spent; otherwise output "Checkmate".

Input example 1:

R.B.QB.R
........
........
........
........
........
........
.K......

Output sample 1:

4

Input example 2:

....RR.Q
........
.K......
........
........
........
........
........

Sample output 2:

Checkmate
#include <iostream>
#include <queue>

using namespace std;

const int M = 8;
const int N = 8;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P; //定义一个坐标形式
char MAP[M][N + 1];
int sx, sy;
int ex, ey;

int d[M][N];
int dx[8] = {
    
    -2, -1, 1, 2, -2, -1, 1, 2}, dy[8] = {
    
    1, 2, 2, 1, -1, -2, -2, -1};

void read_mark();
void bfs();

int main()
{
    
    

    read_mark();
    bfs();
    if(d[ex][ey] != INF)
    cout << d[ex][ey] << endl;
    else
    {
    
    
        cout << "Checkmate" << endl;
    }
    
}

void read_mark()
{
    
    
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
        {
    
    
            cin >> MAP[i][j];
        }

    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
        {
    
    
            if (MAP[i][j] == 'K')
            {
    
    
                sx = i;
                sy = j;
            }

            if (MAP[i][j] == 'Q')
            {
    
    
                ex = i;
                ey = j;
            }

            if (MAP[i][j] == 'R') //战车攻击范围
                {
    
    
                    for (int ii = 0; ii < 8; ii++)
                {
    
    
                    if(MAP[i][ii] == 'Q' ||( MAP[i][ii] == 'R' && ii !=j) ||MAP[i][ii] == 'B' )
                        {
    
    break;}
                    else{
    
    
                        MAP[i][ii] = 'x';
                        
                    }
                     
                }
                for (int ii = 0; ii < 8; ii++)
                {
    
    
                    if(MAP[ii][j] == 'Q' ||MAP[ii][j] == 'B'||( MAP[ii][j] == 'R' && ii !=i))
                        break;
                    else {
    
    MAP[ii][j] = 'x';}
                }
                }
            

            if (MAP[i][j] == 'B') //主教攻击范围
            {
    
    
                
                int I = i+1, J = j+1;
                while (I < M && J <N && I >= 0 && J >= 0 && MAP[I][J] != 'B'&& MAP[I][J] != 'R'&& MAP[I][J] != 'Q')
                {
    
     MAP[I++][J++] = 'x';};
                
                I = i-1;J = j-1;
                while (I < M && J < N && I >= 0 && J >= 0 && MAP[I][J] != 'B'&& MAP[I][J] != 'R'&& MAP[I][J] != 'Q')
                {
    
    MAP[I--][J--] = 'x';};

                I = i-1;J = j+1;
                while (I < M && J < N && I >= 0 && J >= 0 && MAP[I][J] != 'B'&& MAP[I][J] != 'R'&& MAP[I][J] != 'Q')
                {
    
    MAP[I--][J++] = 'x';};

                I = i+1;J = j-1;
                while (I < M && J < N && I >= 0 && J >= 0 && MAP[I][J] != 'B'&& MAP[I][J] != 'R'&& MAP[I][J] != 'Q')
                {
    
    MAP[I++][J--] = 'x';};
            }
        }
    /*for (int i = 0; i < N; i++)
        {putchar('\n');
		for (int j = 0; j < M; j++)
        cout<<MAP[i][j];}*/
}

void bfs()
{
    
    
    queue<P> que;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			d[i][j] = INF;
   que.push(P(sx, sy));  //输入起点
	d[sx][sy] = 0;	  

    while (que.size())  
	{
    
    
		P p = que.front(); que.pop();
		int i;
		for (i = 0; i < 8; i++)		
		{
    
    
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if (0 <= nx&&nx < N
				&& 0 <= ny&&ny < M
				&& MAP[nx][ny] != 'x'
                && MAP[nx][ny] != 'B'
                && MAP[nx][ny] != 'R'
				&& d[nx][ny] == INF)
			{
    
    
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1; 
				if(nx==ex && ny==ey) break; 
 
            }
		}
		if(i!=8) break;
	}
 
}
/*
R...R..Q
B.......
........
........
........
........
........
....K...

*/

Guess you like

Origin blog.csdn.net/m0_46182401/article/details/105716791