AOJ 2083 Black Force

21 篇文章 0 订阅
博客探讨了AOJ 2083问题,通过使用广度优先搜索(BFS)进行解决。初始方案由于在循环内使用栈导致超时(TLE),后来通过加强剪枝和使用数组替代STL,运行时间从5.06s降低到2.16s,实现了性能翻倍。文章讨论了搜索方法的优化技巧。
摘要由CSDN通过智能技术生成

题目有点长, 其实还是很好理解的, 有兴趣的自己去看吧...

这里使用了bfs搜索解决, 因为stack开到了while内部所以贡献了本题的唯一一个TLE...orz 吸取教训

运行时间5.06s


/*author: birdstorm*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>

#define MAXN 205
#define N 105
#define inf 1.0e20
#define eps 1.0e-10
#define MOD 1000000007

#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long

using namespace std;
int h, w, cap, a[24][24];
int move[4][2]={0,1,-1,0,0,-1,1,0};
bool vis[24][24], inh[24][24];
vector<int> ans;
stack<int> s;

bool bfs(int height)
{
    memset(vis,false,sizeof vis);
    rep(i,1,h) rep(j,1,w){
        if(!vis[i][j]&&a[i][j]<height){
            vis[i][j]=true;
            int sum=0;
            while(!s.empty()) s.pop(); //如果stack开在这里就超时... 以后还是用数组吧...
            bool flag=false; //边界判定
            s.push(i*w+j-1);
            while(!s.empty()){
                int t=s.top(); s.pop();
                int x=t/w, y=t%w+1;
                sum+=height-a[x][y]; //累加capacity
                if(inh[x][y]||x<=1||x>=h||y<=1||y>=w) flag=true; //到达边界
                For(m,0,4){
                    int tx=x+move[m][0], ty=y+move[m][1];
                    if(tx>=1&&tx<=h&&ty>=1&&ty<=w&&!vis[tx][ty]&&a[tx][ty]<height) s.push(tx*w+ty-1), vis[tx][ty]=true;
                }
            }
            if(!flag&&sum>=cap) return true;
        }
    }
    return false;
}

int main()
{
    int n, x, y;
    while(scanf("%d%d%d%d",&h,&w,&cap,&n),h){
        ans.clear();
        memset(inh,false,sizeof inh);
        rep(i,1,h) rep(j,1,w) scanf("%d",&a[i][j]), ans.push_back(a[i][j]), ans.push_back(a[i][j]+1);
        while(n--) scanf("%d%d",&x,&y), inh[x][y]=true;
        sort(ans.begin(),ans.end());
        vector<int>::iterator here=unique(ans.begin(),ans.end());
        ans.erase(here,ans.end()); //高度序列去重
        vecfor(iter,ans){
            if(bfs(*iter)) goto end;
            rep(i,1,h) rep(j,1,w) if(!inh[i][j]){
                a[i][j]++; //补上ground work;
                if(bfs(*iter)) goto end; //使用bfs搜索这种方案是否可行
                a[i][j]--; //修改回原始值
            }
        }
        puts("No"); continue;
        end: puts("Yes");
    }
    return 0;
}



使用了强剪枝+数组实现STL后, 速度提高了一倍, 运行时间为2.16s, 不知道那些零点几秒的用的是什么搜索方法


/*author: birdstorm*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>

#define MAXN 205
#define N 105
#define inf 1.0e20
#define eps 1.0e-10
#define MOD 1000000007

#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long

using namespace std;
int h, w, cap, a[24][24];
int move[4][2]={0,1,-1,0,0,-1,1,0};
bool vis[24][24], inh[24][24];
int ans[801];
int s[405];
/*数组代替stl*/

bool bfs(int height)
{
    memset(vis,false,sizeof vis);
    rep(i,1,h) rep(j,1,w){
        if(!vis[i][j]&&a[i][j]<height){
            vis[i][j]=true;
            int sum=0;
            bool flag=false; //边界判定
            int head=0, tail=0;
            s[tail++]=i*w+j-1;
            while(head<tail){
                int x=s[head]/w, y=s[head]%w+1;
                head++;
                sum+=height-a[x][y]; //累加capacity
                if(inh[x][y]||x<=1||x>=h||y<=1||y>=w) flag=true; //到达边界
                For(m,0,4){
                    int tx=x+move[m][0], ty=y+move[m][1];
                    if(tx>=1&&tx<=h&&ty>=1&&ty<=w&&!vis[tx][ty]&&a[tx][ty]<height) s[tail++]=tx*w+ty-1, vis[tx][ty]=true;
                }
            }
            if(!flag&&sum>=cap) return true;
        }
    }
    return false;
}

int main()
{
    int n, x, y;
    while(scanf("%d%d%d%d",&h,&w,&cap,&n),h){
        int tot=0;
        memset(inh,false,sizeof inh);
        rep(i,1,h) rep(j,1,w) scanf("%d",&a[i][j]), ans[tot++]=a[i][j], ans[tot++]=a[i][j]+1;
        while(n--) scanf("%d%d",&x,&y), inh[x][y]=true;
        sort(ans,ans+tot);
        For(m,0,tot){
            if(m&&ans[m]==ans[m-1]) continue;
            if(bfs(ans[m])) goto end;
            rep(i,1,h) rep(j,1,w) if(!inh[i][j]&&a[i][j]+1>=ans[m]){ //剪枝,此点高度+1比预估高度还低时, 不可能是坝的边界
                a[i][j]++; //补上ground work;
                if(bfs(ans[m])) goto end; //bfs搜索这种方案是否可行
                a[i][j]--; //修改回原始值
            }
        }
        puts("No"); continue;
        end: puts("Yes");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值