思维orz 学长YYDS拿捏题意:总(1,1)出发,每次只能向右或者向下(不能走回头路),进一步的,可以以 x为第一关键字,y为第二关键字排序,判断一下 y 是否是非递减的即可

本文介绍了一种使用广度优先搜索(BFS)算法来在给定的地图中寻找特定数量的可通行单元格的解决方案。通过队列操作和路径标记,博主展示了如何计算并判断是否能到达k个目标位置。核心代码片段和关键数据结构如地图、访问标记和计数变量都被详细讨论。
摘要由CSDN通过智能技术生成
#include<bits/stdc++.h>
using namespace std;
int d[2][2] = {{0,1},{1,0}}; //两个方向

const int N = 10005;
int mp[N][N]={0};                //地图
int vis[N][N]={0};            //标记是否搜过
int cn=0;
void bfs(int n,int m,int k) {

    queue<pair<int, int>> q;
    q.push({1,1});
    vis[1][1] = 1;
    if( mp[1][1]==1 ) cn++;
    
    while (q.size()) {
        pair<int, int> t = q.front();                ///入队列出队列巨费时间,时间远不止O(
        q.pop();
        int tx = t.first, ty = t.second;
        
        for (int i = 0; i < 2; i++) {    //扩展(tx,ty)的2个邻居
            int nx = tx + d[i][0], ny = ty + d[i][1];
            
            if( vis[nx][ny] == 0 && nx>=1 && nx<=n && ny>=1 && ny<=m ) {
                
                vis[nx][ny] = 1;  //注意:这一句必不可少
                q.push({nx, ny});
                if( mp[nx][ny]==1 )  cn++; 
            }
         }
       }
    
    if( cn==k ) cout<<"YES\n";
    else cout<<"NO\n";
}

int main() {
    int T;cin>>T;
    while( T-- ) {
        int n,m,x,y,k;
        cin>>n>>m>>k;
        mp[n+5][m+5]={0};//初始化
        vis[n+5][m+5]={0};//初始化
        for(int i=0;i<k;i++) {       O(K)
            cin>>x>>y; 
            mp[x][y]=1;
        }
        cn=0;//初始化
        bfs(n ,m, k);       //O(2K)
    }
    return 0;
}
bool cmp(node a ,node b){             //行数升序排列
    if(a.x!=b.x)return a.x<b.x;
    return a.y<b.y;
}
int main(){
    std::ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0)

    ll n,m,k,ok=1; cin>>n>>m>>k;

        for(int i=1;i<=k;i++){    //2*1e5
            int x,y;
            cin>>x>>y;
            t[i]={x,y};
        }

        sort(t+1,t+1+k,cmp);        //2*1e5*(1+log2 1e5)
        int ed=t[1].y;

        for(int i=2;i<=k;i++){      //2*1e5

            if(t[i].y<ed){          //一旦出现y值减小的格子,铁定取不完了
                ok=0;
                break;
            }
            else 
                ed=t[i].y;

        }

        if(ok)cout<<"YES\n";

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值