Week4

1-1题目描述

给定一个 N *M 方格的迷宫,迷宫里有 T 处障碍,障碍处不可通过。

在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。

给定起点坐标和终点坐标,每个方格最多经过一次,问有多少种从起点坐标到终点坐标的方案。

输入格式

第一行为三个正整数 N,M,T,分别表示迷宫的长宽和障碍总数。

第二行为四个正整数 SX,SY,FX,FY,SX,SY 代表起点坐标,FX,FY代表终点坐标。

接下来 T 行,每行两个正整数,表示障碍点的坐标。

输出格式

输出从起点坐标到终点坐标的方案总数。

样例 #1

样例输入 #1

2 2 1
1 1 2 2
1 2

样例输出 #1

1

提示

对于 100\%的数据,1 <=N,M <=5,1 <= T <=10,1 <=SX,FX <=n,1 <=SY,FY <=m

解题思路:dfs

完整代码如下:

#include <bits/stdc++.h>
using namespace std;
​
const int maxn=1e4+5;
​
int n,m,t,sx,sy,fx,fy,ans=0;
​
int mi[maxn][maxn]={0};
​
bool vis[maxn][maxn]={false};//标记是否经过 
int s_x[5]={1,0,-1,0};
int s_y[5]={0,-1,0,1};  //来实现上下左右移动 
​
void dfs(int x,int y);
​
int main()
{
    cin>>n>>m>>t;
    cin>>sx>>sy>>fx>>fy;
    mi[fx][fy]=1; // 1表示终点
    vis[sx][sy]=true;
    int x,y;
    for(int i=1; i<=t; i++){
        cin>>x>>y;
        mi[x][y]=-1; // -1表示障碍 
    } 
    dfs(sx,sy);
    cout<<ans;
    return 0;
}
​
void dfs(int x,int y)
{
     if(mi[x][y]==1){
        ans++;
        return;
    }
    for(int i=0; i<5; i++){
        int next_x=x+s_x[i];
        int next_y=y+s_y[i];
        if(next_x<1 || next_x>n || next_y<1 || next_y>m || mi[next_x][next_y]==-1 || vis[next_x][next_y]) continue;
        //判断是否越界,是否走过 
        vis[next_x][next_y]=true;
        dfs(next_x,next_y);
        vis[next_x][next_y]=false; //回溯 
    }
}

1-2题目描述

有一个 n *m的棋盘,在某个点 (x, y) 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步。

输入格式

输入只有一行四个整数,分别为 n, m, x, y。

输出格式

一个 n*m的矩阵,代表马到达某个点最少要走几步(不能到达则输出 -1)。

样例 #1

样例输入 #1

3 3 1 1

样例输出 #1

0    3    2    
3    -1   1    
2    1    4

提示

数据规模与约定

对于全部的测试点,保证 1 <= x <= n <+400,1 <=y <=m <= 400

解题思路:bfs

完整代码如下:

#include <bits/stdc++.h>
using namespace std;
​
const int maxn=1e4+5;
​
int n,m,x,y;
​
int sx[8]={-2,-2,-1,-1,1,1,2,2};
int sy[8]={1,-1,2,-2,2,-2,1,-1}; //马的走法 
​
bool vis[maxn][maxn];//标记是否到达
int num[maxn][maxn];//记录最小步数 
​
queue< pair<int,int> > q;
​
int main()
{
    cin>>n>>m>>x>>y;
    q.push(make_pair(x,y));
    while(!q.empty()){
        int nx=q.front().first;
        int ny=q.front().second;
        q.pop();
        vis[nx][ny]=true;
        for(int i=0; i<8; i++){
            int next_x=nx+sx[i];
            int next_y=ny+sy[i];
            if(next_x>=1 && next_x<=n && next_y>=1 && next_y<=m && !vis[next_x][next_y]) //判断是否越界和标记 
            {
                q.push(make_pair(next_x,next_y));
                num[next_x][next_y]=num[nx][ny]+1;
                vis[next_x][next_y]=true;
            }
        } 
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(!vis[i][j]) cout<<"-1"<<" ";
            else cout<<num[i][j]<<" ";
        }
        cout<<endl;
    } 
    return 0;
}
​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值