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. 表格中的checkbox复选框 全选非全选 公共方法 及提交选中结果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. Day22 常用模块01

    1. collections模块collections模块主要封装了⼀些关于集合类的相关操作. 比如, 我们学过的Iterable,Iterator等等. 除了这些以外, collections还提供 ...

  3. 1. github配置

    1. 安装:官网傻瓜式安装 2.密钥的生成:为了不让不想干的人提交代码,所以需要一个密钥 执行这个命令 : ssh-keygen -t rsa -C "邮箱地址" 然后一直回车键回 ...

  4. [USACO13FEB] Tractor

    题目链接 大家的 Blog 里面都是做过的题目,只有我的里面什么都没有 那我也开始写一点吧 刷着刷着 DP 不知怎的就出来一道这个题……用时 2 hours 后怒而删两个文件重构…… 然后过了……过了 ...

  5. nginx安装配置: configure命令

    configure命令用来配置nginx编译环境. 该命令定义了系统各方面功能,包括允许nginx使用的连接处理方式. 其执行结果是生成一个Makefile文件. configure命令支持如下参数: ...

  6. tcping 与 telnet命令粗略使用

        使用tcping命令,在网上下载tcping文件,放入c盘的system32目录下,即可使用 使用tcping命令用来ping某个端口,能通的话,说明从外部到端口是没有问题的 使用telnet ...

  7. .gitignore无效的原因

    有时候,我们编写gitinore后发现文件还是没有被忽略,这是什么原因呢? 熟知git的老鸟们可能已经知道,因为这个文件在之前已经被追踪了,如果想忽略已经被追踪的文件我们需要把这个追踪去除. 对所有文 ...

  8. 2017-12-15python全栈9期第二天第四节之格式化输出%s和用户交互个人简历模板

    #!/user/bin/python# -*- coding:utf-8 -*-name = input('姓名:')age = input('年龄:')job = input('工作:')hobbi ...

  9. Python 排序和numpy排序,得到排序后索引序列(及源list的序列)

    Python list 排序 & np list 排序 nums = [1.25, 0.98, 6.13, 7.62] li = np.array(nums) print(li) out = ...

  10. 网络编程基础【day10】:我是一个线程(四)

    本节内容 1.第一回 初生牛犊 2.第二回 渐入佳境 3.第三回 虎口脱险 4.第四回 江湖再见 第一回 初生牛犊 我是一个线程,我一出生就被编了个号:0x3704,然后被领到一个昏暗的屋子里,在这里 ...