P1605 迷宫

这是一道毒瘤题。。。

这是一道广搜题 bfs 。。。

注释:

1.memcpy(b,a,sizeof(a))

把 a 的值全部复制给 b

memcpy(b,a,sizeof(int)*k)

把 a 中的 k 个元素复制给 b

头文件:#include<cstring>

代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
int n,m,t,sx,sy,fx,fy,tx,ty,ans=;
bool vis[][]; //表示是否为墙
int dx[]={-,,,},
dy[]={,,-,};
struct pos //结构体 x代表横坐标,y代表纵坐标,used[][]代表是否走过
{
int x,y,used[][]; };
bool pan(int x,int y) //判断是否合法
{
return x>=&&x<=n&&y>=&&y<=m&&vis[x][y]==;
} pos sa;
void bfs()
{
queue<pos>q; //队列
sa.x=sx;
sa.y=sy;
sa.used[sx][sy]=; //标记已走
q.push(sa); //入队
while(!q.empty())
{
pos h=q.front();
q.pop(); //出队
for(int i=;i<=;i++)
{
int xx=h.x+dx[i];
int yy=h.y+dy[i];
if(h.used[xx][yy]||(!pan(xx,yy))) //不可以走
continue ;
if(xx==fx&&yy==fy) //到终点
{
ans++;
continue ;
} sa.x=xx;
sa.y=yy;
memcpy(sa.used,h.used,sizeof(h.used)); //鬼知道这是干什么哒(我知道了)注释1
sa.used[xx][yy]=;
q.push(sa); //新的入队 }
}
}
int main()
{
cin>>n>>m>>t;
cin>>sx>>sy>>fx>>fy;
for(int i=;i<=t;i++)
{
cin>>tx>>ty;
vis[tx][ty]=;
} bfs(); //广搜 cout<<ans; return ; }

这还是一道深搜题 dfs 。。。

回顾一下递归回溯算法框架:

int search(int x,int y)
{
if(到目的地) 输出解;
else
for(int i=;i<=算符种数;i++)
{
if(符合条件)
{
保存结果;
search(下一层);
恢复:保存结果之前的状态{回溯};
}
}
}

代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
int n,m,t,sx,sy,fx,fy,tx,ty,ans=;
bool vis[][];
int dx[]={-,,,},
dy[]={,,-,};
bool pan(int x,int y)
{
return x>=&&x<=n&&y>=&&y<=m&&vis[x][y]==;
} void dfs(int x,int y)
{
if(x==fx&&y==fy)
ans++; for(int i=;i<=;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(pan(xx,yy))
{
vis[xx][yy]=;
dfs(xx,yy);
vis[xx][yy]=;
}
}
}
int main()
{
cin>>n>>m>>t;
cin>>sx>>sy>>fx>>fy;
for(int i=;i<=t;i++)
{
cin>>tx>>ty;
vis[tx][ty]=;
}
vis[sx][sy]=;
dfs(sx,sy); cout<<ans; return ; }

P1605 迷宫的更多相关文章

  1. 洛谷 P1605 迷宫

    题目链接 https://www.luogu.org/problemnew/show/P1605 题目背景 迷宫 题目描述 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 ...

  2. 洛谷—— P1605 迷宫

    P1605 迷宫 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在 ...

  3. 洛谷P1605 迷宫——S.B.S.

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  4. 【搜索1】P1605 迷宫

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  5. (DFS)P1605 迷宫 洛谷

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  6. P1605 迷宫 dfs回溯法

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  7. 洛谷P1605 迷宫 (DFS)

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  8. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. P1605 迷宫(洛谷)

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...

随机推荐

  1. 【AI】神经网络基本词汇

    neural networks 神经网络activation function 激活函数hyperbolic tangent 双曲正切函数bias units 偏置项activation 激活值for ...

  2. 在cmd启动一个win32程序,printf把信息输出到启运它的那个CMD窗口

    #define ProcessBasicInformation 0 typedef struct { DWORD ExitStatus; DWORD PebBaseAddress; DWORD Aff ...

  3. Redis 教程 Java工程师学习知识点

    1. Redis简介及安装 1.1 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下 ...

  4. jquery操作checkBox 一次取消选中后不能再选中

    $("input[type='checkbox']").each(function(){ $(this).attr("checked","checke ...

  5. vue中嵌套页面 iframe 标签

    vue中嵌套iframe,将要嵌套的文件放在static下面: <iframe src="../../../static/bear.html" width="300 ...

  6. 在linux中安装selenium+chrome

    主要参照百度的一些内容加上自己的实际操作,对自己遇到的几个问题进行总结: 第一个问题:安装selenium---sudo pip install selenium 显示:You are using p ...

  7. Python学习之旅(二十六)

    Python基础知识(25):常用内建模块 1.datetime:处理日期和时间 (1)获取当前日期和时间 from datetime import datetime now = datetime.n ...

  8. python语法_while循环_for循环

    while 循环: while 条件: print('''asdasd') print('''asdasd') print('''asdasd') 当条件为True时,持续循环 当条件为Flase时, ...

  9. AttributeError: 'int' object has no attribute 'isdigit'(python下的isdigit函数)

    python下的isdigit函数:  isdigit() 方法检测字符串是否只由数字组成. 语法 isdigit()方法语法:  str.isdigit() 示例代码如下: 结果: 我想说的重点在于 ...

  10. PAT甲级1135 Is It A Red-Black Tree?【dfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 题意: 给定一棵二叉搜索树的先序遍历结 ...