题意与分析(CodeForces 540C)

这题坑惨了我。。。。我和一道经典的bfs题混淆了,这题比那题简单。

那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落。然后求可行解。

但是这题。。。是冰塔的一层

也就是说,它只是个稍微有点限制的二维迷宫问题。

后面就好理解了,不过需要考虑下这种数据:

1 2
XX
1 1
1 1

这种数据答案是no。解决的方法可以考虑这样:分成两个数组来记录访问状态:vis数组和block数组。

代码

#include <queue>
#include <set>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define ZERO(x) memset((x), 0, sizeof(x))
#define ALL(x) (x).begin(),(x).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define QUICKIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std; bool iscracked[505][505];
bool vis[505][505];
set<pair<int,int> > s; int bx,by,ex,ey,n,m;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
bool bfs()
{
queue<pair<int,int> > q;
q.push(MP(bx,by));
s.insert(MP(bx,by));
iscracked[bx][by]=true;
auto end_pair=MP(ex,ey);
while(!q.empty())
{
auto now=q.front(); q.pop();
if(now==end_pair && iscracked[now.fi][now.se] && vis[now.fi][now.se]) return true;
//cout<<"NP: "<<now.fi<<" "<<now.se<<endl;
iscracked[now.fi][now.se]=true;
vis[now.fi][now.se]=true;
rep(i,0,3)
{
int tx=now.fi+dx[i], ty=now.se+dy[i];
if(tx>=1 && tx<=n && ty>=1 && ty<=m && (!iscracked[tx][ty]||(tx==ex&&ty==ey)))
{
auto tstat=MP(tx,ty);
//cout<<"Trying to go"<<tx<<" "<<ty<<" "<<iscracked[tx][ty]<<endl;
if(s.find(tstat)==s.end() || tstat==end_pair)
{
//cout<<" success"<<endl;
s.insert(tstat);
q.push(tstat);
}
}
}
}
return false;
} int main()
{
cin>>n>>m;
ZERO(iscracked);ZERO(vis);
rep(i,1,n)
{
string str; cin>>str;
rep(j,0,m-1)
{
iscracked[i][j+1]=vis[i][j+1]=str[j]=='X';
}
}
cin>>bx>>by>>ex>>ey;
vis[bx][by]=false;
//if(bx==ex && by==ey && n==1 && m==1)
cout<<string(bfs()?"YES":"NO")<<endl;
return 0;
}

最新文章

  1. Web.config自定义节点configSections
  2. POJ 1236 Network of Schools(强连通分量/Tarjan缩点)
  3. C语言指针传递详解
  4. hdu5381 The sum of gcd
  5. Mysql压测工具mysqlslap 讲解
  6. SOLVED: GATT callback fails to register
  7. dataTable 禁止分页
  8. 前端——HTML笔记-One
  9. sql中判断是否存在某个对象
  10. 读书笔记 effective c++ Item3 在任何可能的时候使用 const
  11. 图像处理------Canny边缘检测
  12. 通用查询类封装之Mongodb篇
  13. windows docker redis
  14. linux下的ssh和rynsc
  15. TCP的三次握手与四次挥手理解及面试题(很全面)
  16. vlc 网页插件的 使用与控制 API http://www.xuebuyuan.com/2224602.html
  17. Vanish/squid
  18. 四种对象生存期和作用域、static 用法总结
  19. PowerDesigner生成Oracle表名带有引号的解决方法
  20. 16-oauth2-oidc-Client实现

热门文章

  1. PHP语言开发微信公众平台(订阅号)之curl命令(补充)
  2. redis 哈希数据类型简单操作(实现购物车案例)
  3. 在Windows 7和10上显示上次登录帐户信息
  4. springboot+mybatis+shiro——登录认证和权限控制
  5. HUD 1288 Hat&#39;s Tea(反向的贪心,非常好的一道题)
  6. 提示AttributeError: &#39;module&#39; object has no attribute &#39;HTTPSHandler&#39;解决方法
  7. oracle 的replace()
  8. 纯 js 实现跨域接口调用 jsonp
  9. IDEA一直提示 错误: 找不到或无法加载主类
  10. 解决IDEA打印到控制台的中文内容乱码