一、问题:

输入:

6 7
0 1 1 1 0 0 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 1 0
1 1 1 0 1 0 0
1 1 1 1 0 0 0

输出:

4

代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstdlib>
using namespace std;

struct node{
    int x,y;
}Node;

void BFSNUM(int x,int y);
bool judge(int x,int y);

int temp[100][100];
bool inq[100][100] = {false};
int X[4]={0,0,1,-1};
int Y[4]={1,-1,0,0};
int n,m;

int main(){
    
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>temp[i][j];
        }
    }
    int num=0;  
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(temp[i][j]==1&&inq[i][j]==false){
                num++;
                BFSNUM(i,j);
            }
        }
    }
    cout<<num<<endl;
    return 0;
}

void BFSNUM(int x,int y){
    queue<node> q;
    Node.x = x,Node.y = y;
    q.push(Node);
    inq[x][y] = true;
    while(!q.empty()){
        node top = q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int newX = top.x+X[i];
            int newY = top.y+Y[i];
            if(judge(newX,newY)){
                Node.x=newX,Node.y=newY;
                q.push(Node);
                inq[newX][newY] = true; 
            }
        }
    }
}

bool judge(int x,int y){
    if(x>n||x<0||y>m||y<0){
        return false;
    }
    if(temp[x][y]!=1||inq[x][y]==true){
        return false;
    }
    return true;
}

二、问题:

输入:

5 5
. . . . .
. * . * .
. * S * .
. * * * .
. . . T *
2 2 4 3

输出:

11

代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

struct node{
    int x,y;
    int step;
}S,T,Node;

void BFSNUM();
bool judge(int x,int y);

char temp[100][100];
bool inq[100][100] = {false};
int X[4]={0,0,1,-1};
int Y[4]={1,-1,0,0};
int n,m;

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){    
        for(int j=0;j<m;j++){
           cin>>temp[i][j]; 
        } 
    }
    cin>>S.x>>S.y>>T.x>>T.y;
    S.step = 0;
    BFSNUM();
    return 0;
}

void BFSNUM(){
    queue<node> q;
    q.push(S);
    while(!q.empty()){
        node top = q.front();
        q.pop();
        if(top.x==T.x&&top.y==T.y){
            
            cout<<top.step<<endl;
        }
        for(int i=0;i<4;i++){
            int newX = top.x+X[i];
            int newY = top.y+Y[i];
            if(judge(newX,newY)){
                Node.x = newX,Node.y=newY;
                Node.step = top.step+1;
                q.push(Node);
                inq[newX][newY] = true;
            }
        }
    }
}

bool judge(int x,int y){
    if(x>n||x<0||y>m||y<0){
        return false;
    }
    if(temp[x][y]=='*'||inq[x][y]==true){
        return false;
    }
    return true;
}

三、问题:马的移动

题目描述

小明很喜欢下国际象棋,一天,他拿着国际象棋中的“马”时突然想到一个问题:
给定两个棋盘上的方格a和b,马从a跳到b最少需要多少步?
现请你编程解决这个问题。

提示:国际象棋棋盘为8格*8格,马的走子规则为,每步棋先横走或直走一格,然后再往外斜走一格。

输入

输入包含多组测试数据。每组输入由两个方格组成,每个方格包含一个小写字母(a~h),表示棋盘的列号,和一个整数(1~8),表示棋盘的行号。

输出

对于每组输入,输出一行“To get from xx to yy takes n knight moves.”。

样例输入 Copy

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

样例输出 Copy

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

//每步棋先横走或直走一格,然后再往外斜走一格。
struct node{
    int x,y,step;
};

void BFS();
bool judge(int x,int y);

int temp[8][8];
bool inq[8][8];
int to[8][2]={-1,-2,-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2};
int sx,sy,ex,ey,ans;

int main(){ 
    char c1[10],c2[10];
    while(cin>>c1>>c2){
        sx = c1[0]-'a';
        sy = c1[1]-'1';
        ex = c2[0]-'a';
        ey = c2[1]-'1';
        BFS();
        printf("To get from %s to %s takes %d knight moves.\n",c1,c2,ans);
    }
    return 0;
}

void BFS(){
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            temp[i][j]=0;
            inq[i][j]=false;
        }
    }
    node S;
    S.x = sx;
    S.y = sy;
    S.step=0;
    queue<node> q;
    q.push(S);
    inq[sx][sy] = true;
    while(!q.empty()){
        node v = q.front();
        q.pop();
        if(v.x == ex&&v.y == ey){
            ans = v.step;
        }
        for(int i=0;i<8;i++){
            int newX = v.x +to[i][0];
            int newY = v.y +to[i][1];
            if(judge(newX,newY)){
                node newnode;
                newnode.x = newX; 
                newnode.y = newY; 
                newnode.step = v.step+1;
                q.push(newnode);
                inq[newX][newY] = true; 
            }
        }
    }
}


bool judge(int x,int y){
    if(x<0||x>=8||y<0||y>=8||inq[x][y]==true){
        return false;
    }
    return true;
}

 

posted on 2020-02-28 17:31  晨曦生辉耀匕尖  阅读(160)  评论(0编辑  收藏  举报