A - 棋盘问题

POJ - 1321

注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了。

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

POJ - 2251

注意条件:简单三维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

POJ - 3278

注意条件: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

POJ - 3279

注意条件:直接枚举第一行,然后通过下面的一行调整为他的上一行全为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带你飞]专题一 简单搜索(回顾)的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 回顾总结

    第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队

  2. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  3. [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

    //Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...

  4. [kuangbin带你飞]专题一 简单搜索 棋盘问题

    题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...

  5. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  6. 迷宫问题 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, ...

  7. [kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...

  8. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  9. 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 ...

随机推荐

  1. ElasticSearch6.3.2------查询

    进入Kibana的控制台:http://localhost:5601/app/kibana#/dev_tools/ 先放一些测试数据进去,不想一条一条,就用bulk 注意格式 正确格式: 解释:ES期 ...

  2. java中异常的面试

    https://blog.csdn.net/qq_36523638/article/details/79363652 1) Java中的检查型异常和非检查型异常有什么区别? 这又是一个非常流行的Jav ...

  3. (贪心 模拟?) codeVs1098 均分纸牌

    题目描述 Description 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...

  4. vue(基础一)_基本指令的使用

    一.前言 1.基本骨架                          2.插值表达式{{ }}   3.vue起的作用,在开发中充当的角色MVC                           ...

  5. opencv: 角点检测源码分析;

    以下6个函数是opencv有关角点检测的函数 ConerHarris, cornoerMinEigenVal,CornorEigenValsAndVecs, preConerDetect, coner ...

  6. numpy知识点

    1.nonzero 对于一维数据来说,将返回符合条件的 下标 >>> b1 = np.array([True, False, True, False]) >>> n ...

  7. Shiro中session超时页面跳转的处理

    问题描述 shiro在管理session后,在session超时会进行跳转,这里有两种情况需要考虑,一种是ajax方式的请求超时,一种页面跳转请求的超时. 本文从这两个方面分别考虑并处理. ajax请 ...

  8. Linux下常用的shell操作

    # 设定hosts解析记录 sh-4.2# echo "$(ifconfig ens192 | awk '/\<inet\>/{print $2}') $(hostname)&q ...

  9. Java中的XML

    XML是一种可扩展的标记语言,可扩展就是<>内的东西可以自己定义,可以随便写.标记语言就是加了<>符号的 .HTML是超文本标记语言,不可以拓展,因为你写个<p> ...

  10. Java NIO系列教程(八)JDK AIO编程

    目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...