题目有点长, 其实还是很好理解的, 有兴趣的自己去看吧...
这里使用了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;
}