[kuangbin带你飞]专题一 简单搜索(回顾)
注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了。
AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
# define ll long long
const int maxn =;
char str[maxn][maxn];
int vis[maxn];
int n,m,num;
void dfs(int u,int cnt)
{
if(cnt==m)
{
num++;
return ;
}
if(u==n)
return ;
for(int i=; i<n; i++)
{
if(!vis[i]&&str[u][i]=='#')
{
vis[i]=;
dfs(u+,cnt+);
vis[i]=;
}
}
dfs(u+,cnt);
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==-&&m==-)
break;
memset(vis,,sizeof(vis));
num=;
for(int i=; i<n; i++)
{
scanf("%s",str[i]);
}
dfs(,);
printf("%d\n",num);
}
return ;
}
B - Dungeon Master
注意条件:简单三维bfs。
AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
# define ll long long
const int maxn =;
char str[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int f[][]= {{,-,,,,},
{,,-,,,},
{,,,,,-}
};
int n,m,k;
int stx,sty,stz;
int edx,edy,edz;
int ans;
bool judge(int x,int y,int z)
{
if(x>=&&x<=n&&y>=&&y<=m&&z>=&&z<=k)
return true;
return false;
}
struct node
{
int x,y,z,step;
node() {}
node(int xx,int yy,int zz,int tt)
{
x=xx,y=yy,z=zz,step=tt;
}
};
void bfs(int x,int y,int z)
{
queue<node>q;
q.push(node(x,y,z,));
vis[x][y][z]=;
while(!q.empty())
{
node top=q.front();
q.pop();
if(top.x==edx&&top.y==edy&&top.z==edz)
{
ans=top.step;
return ;
}
for(int i=; i<; i++)
{
int tx,ty,tz;
tx=top.x+f[][i];
ty=top.y+f[][i];
tz=top.z+f[][i];
if(judge(tx,ty,tz)&&vis[tx][ty][tz]==&&str[tx][ty][tz]!='#')
{
vis[tx][ty][tz]=;
q.push(node(tx,ty,tz,top.step+));
}
}
}
}
int main()
{
while(~scanf("%d %d %d",&n,&m,&k)&&(n+m+k))
{
memset(vis,,sizeof(vis));
ans=-;
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
scanf("%s",str[i][j]+);
for(int l=; l<=k; l++)
{
if(str[i][j][l]=='S')
{
stx=i;
sty=j;
stz=l;
}
if(str[i][j][l]=='E')
{
edx=i;
edy=j;
edz=l;
}
}
}
}
bfs(stx,sty,stz);
if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return ;
}
C - Catch That Cow
注意条件:bfs,注意限制条件。
AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
# define ll long long
const int maxn =2e5+;
int vis[maxn];
pair<int,int> top;
int bfs(int st,int ed){
queue<pair<int,int> >q;
vis[st]=;
q.push(make_pair(st,));
while(!q.empty()){
top=q.front();
q.pop();
if(top.first==ed){
return top.second;
}
if(top.first+<=ed&&vis[top.first+]==){
vis[top.first+]=;
q.push(make_pair(top.first+,top.second+));
}
if(top.first->=&&vis[top.first-]==){
vis[top.first-]=;
q.push(make_pair(top.first-,top.second+));
}
if(top.first*<=*ed&&vis[top.first*]==){
vis[top.first*]=;
q.push(make_pair(top.first*,top.second+));
}
}
}
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
memset(vis,,sizeof(vis));
int ans=bfs(n,m);
printf("%d\n",ans);
}
return ;
}
D - Fliptile
注意条件:直接枚举第一行,然后通过下面的一行调整为他的上一行全为0.找出最小步骤,如果存在多个最小步骤相等。找出字典序最小的(对第一行二进制枚举就可以保证了)
AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = ;
int a[maxn][maxn];
int tmp[maxn][maxn];
int ans[maxn][maxn];
int com[maxn][maxn];
int n,m,ti;
void flip(int x,int y){
ti++;
tmp[x][y]^=;
tmp[x+][y]^=;
tmp[x-][y]^=;
tmp[x][y+]^=;
tmp[x][y-]^=;
}
bool judge(int t){
memcpy(tmp,a,sizeof(a));
memset(com,,sizeof(com));
for(int i=m;i>=;i--){
com[][m-i+]=((t&(<<(i-)))>?:);
}
for(int i=;i<=m;i++){
if(com[][i])flip(,i);
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(tmp[i-][j])flip(i,j),com[i][j]=;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(tmp[i][j])return false;
}
}
return true;
}
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
int maxstate=(<<m)-;
int maxx=inf;
for(int i=;i<=maxstate;i++){
ti=;
if(judge(i)){
if(maxx>ti){
maxx=ti;
memcpy(ans,com,sizeof(com));
}
}
}
if(maxx==inf){
printf("IMPOSSIBLE\n");
}
else {
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(j==)printf("%d",ans[i][j]);
else printf(" %d",ans[i][j]);
}
printf("\n");
}
}
return ;
}
//溜了,回家玩耍的
[kuangbin带你飞]专题一 简单搜索(回顾)的更多相关文章
- [kuangbin带你飞]专题一 简单搜索 回顾总结
第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队
- [kuangbin带你飞]专题一 简单搜索 题解报告
又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...
- [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple
//Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...
- [kuangbin带你飞]专题一 简单搜索 棋盘问题
题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...
- [kuangbin带你飞]专题一 简单搜索
ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题 328 / 854 Problem B POJ 2251 Dungeon Ma ...
- 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- [kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
随机推荐
- PMP证书的获取,不知道10大注意事项会吃亏
作为一个已经考过PMP的小项目经理我来说,近来接到不少咨询PMP的,有咨询考试事宜的,也有咨询后续的换审和PDU的,今天我这边就说说PMP项目管理证书要获取的一些注意事项,不注意的话可是会吃大亏的. ...
- strutrs contextMap
contextMap(非常重要) 1.动作类的生命周期 明确:动作类是多例的,每次动作访问,动作类都会实例化.所以是线程安全的.与Struts1的区别是,struts1的动作类是单例的. 2.请求动作 ...
- Java如何判断文件或者文件夹是否在?不存在如何创建?
Java如何判断文件或者文件夹是否在?不存在如何创建? 1. 首先明确一点的是:test.txt文件可以和test文件夹同时存在同一目录下:test文件不能和test文件夹同时存在同一目录下. 原 ...
- Vuex速学篇:(2)利用state保存新闻数据
回顾 以前我们在做这个新闻列表的时候,是一个写死的数据 export default{ data(){ return{ newslist:[ {newsid:"101",pubti ...
- POJ3417 LCA+树dp
http://poj.org/problem?id=3417 题意:先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂 ...
- Dream team: Stacking for combining classifiers梦之队:组合分类器
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- my live health
s 盐城的社保咨询服务热线电话:12333 射阳县医疗保险基金中心地址:射阳县合德镇解放东路24号 电话:0515-82322433 社保办事指南:http://yancheng.bendibao.c ...
- 【SQL】数据库中的五种约束
#五大约束 1.主键约束(Primay Key Coustraint) 唯一性,非空性 2.唯一约束 (Unique Counstraint)唯一性,可以空,但只能有一个 3.检查约束 (Check ...
- html基本进阶知识【转】
inline和block的区别: 网页一般是两种元素组合起来的,一种是内联元素,也就是行内显示,加上width和height没效果.一种是区块元素,可以加上对应的width和height,通常使用在网 ...
- ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)
https://nanti.jisuanke.com/t/31721 题意 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同:2.若三种食物都有的情况,巧克力不能在中 ...