2019CCPC河北省赛 L smart robot

就纯纯的跑dfs,只不过就是得分析分析题面

#include<iostream>
#include<bits/stdc++.h>
#include<queue>
#include<set>
using namespace std;
const int maxn=62;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
set<int>s;
int n,a[maxn][maxn];
void dfs(int x,int y,int num,int dep){
    s.insert(num);
    if(dep){
        for(int d=0;d<4;d++){
            int nx=x+dx[d],ny=y+dy[d];
            if(1<=nx && nx<=n && 1<=ny && ny<=n)
                dfs(nx,ny,num*10+a[nx][ny],dep-1);
        }
    }   
}

int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>a[i][j];
    
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            dfs(i,j,a[i][j],5);
    int ans=0;
    while(s.find(ans)!=s.end())ans++;
    cout<<ans<<endl;

    return 0;
}

当然BFS也行,就是当心爆内存。

#include<iostream>
#include<algorithm>
#include <queue>
#include <map>
#include<bits/stdc++.h>
using namespace std; 
const int stp =5;
const int maxn = 50 + 5;
int mp[maxn][maxn];
map<long long, bool> mapp;
const int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int vis[maxn][maxn];
inline bool jdg(int x,int y, int n){ return x >= 1 && y >= 1 && x <= n && y <= n;}
struct node{
    int x,y;
    long long nm;
    int cnt;
    node(int xx,int yy, long long n,int c) : x(xx), y(yy),nm(n),cnt(c){}
};
void bfs(int fx, int fy,int n) {
    memset(vis,0,sizeof vis);
    queue<node> que;
    que.push(node(fx, fy,mp[fx][fy],1));
    while (!que.empty()) {
        node it = que.front();
        que.pop();
        mapp[it.nm] = true;
        for (int i = 0; i < 4; ++i) {
            int dx = dir[i][0] + it.x;
            int dy = dir[i][1] + it.y;
            if (jdg(dx,dy,n) && it.cnt <= stp){
                que.push(node(dx,dy,it.nm * 10 + mp[dx][dy],it.cnt + 1));
            }
        }
    }
}
int main() {
    int t;
    cin >> t;
    for (int i = 1; i <= t; ++i) 
        for (int j = 1; j <= t; ++j) 
            cin >> mp[i][j];
          
     for (int i = 1; i <= t; ++i) 
       for (int j = 1; j <= t; ++j) 
            bfs(i,j,t);     
    int cur=0;
    for(int i=0;;i++){
    	if(mapp[i]==true) cur++;
		else {
			cout <<cur<<endl;	
			break;
		}
	}
    return 0;
}
发布了183 篇原创文章 · 获赞 31 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/mlm5678/article/details/90580265