POJ1321棋盘问题(暴搜)
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
乍一看和八皇后类似,但其实不太一样。和网上思路不太一样,这个题其实可以看成是:把相同的棋子以某种方式放进给定的格点里,求放入方式,可以转化成求全排列+判定,用DFS搞过。(全排列暴搜参考蓝书相关代码)。
跑了922ms,在T的边缘疯狂试探。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int n,k;
char mmap[][];
struct node
{
int x;
int y;
}nod[];
bool vis[];
vector<node>v;
int ans=;
void dfs(int x,int cnt)//x表示选的序号 cnt表示已经用的棋子 //从vector里选出m个符合要求的位置
{
if(cnt>k||(cnt+v.size()-x)<k)
{
return;
}
if(x==v.size())
{
ans++;
return;
}
//不选x
dfs(x+,cnt);
//选x
int i;
bool flag=;
for(i=;i<x;i++)
{
if(vis[i]&&i!=x&&(v[i].x==v[x].x||v[i].y==v[x].y))//判断是否同行同列
{
flag=;
break;
}
}
if(flag)
{
vis[x]=;
dfs(x+,cnt+);
vis[x]=;
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF&&n!=-&&k!=-)
{
memset(mmap,,sizeof(mmap));
memset(vis,,sizeof(vis));
ans=;
int i,j;
for(i=;i<n;i++)
{
scanf("%s",mmap[i]);
}
v.clear();
node non;
non.x=;
non.y=;
v.push_back(non);//为了使vector下标从1开始,放一个空的占位
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
if(mmap[i][j]=='#')//满足条件的格点放入vector
{
node temp;
temp.x=i;
temp.y=j;
v.push_back(temp);
}
}
}
dfs(,);//暴搜
cout<<ans<<endl;
}
}
POJ1321棋盘问题(暴搜)的更多相关文章
- 模拟 + 暴搜 --- Help Me with the Game
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3175 Accepted: ...
- 【BZOJ-3033】太鼓达人 欧拉图 + 暴搜
3033: 太鼓达人 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 204 Solved: 154[Submit][Status][Discuss] ...
- c++20701除法(刘汝佳1、2册第七章,暴搜解决)
20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入正整数n,按从小到大的顺序输出所有 ...
- Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- Sicily1317-Sudoku-位运算暴搜
最终代码地址:https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1317.c 这题博主刷了1天,不是为了做出来,AC ...
- codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Weights Xenia has a set of weig ...
- Usaco 2.3 Zero Sums(回溯DFS)--暴搜
Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...
- HDU4403(暴搜)
A very hard Aoshu problem Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
随机推荐
- access denied (2)
除了https://www.cnblogs.com/bneglect/p/11558593.html 这里提到的以外,今天再记录一个新的原因造成这种情况的解决方法.path_info 这个算是路由模 ...
- PAT-链表-A1032 Sharing
题意:给出两条链表的首地址以及若干个节点的的地址.数据.下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1. 思路:使用静态链表,首先遍历一遍第一个链表并进行标记. ...
- 计算几何-BZOJ2618-凸包的交-HPI
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. bzoj2618 ...
- Educational Codeforces Round 76 (Rated for Div. 2) B. Magic Stick
Recently Petya walked in the forest and found a magic stick. Since Petya really likes numbers, the f ...
- 1.3 使用jmeter进行http接口测试
来源: http://www.cnblogs.com/alisapan/p/6150309.html 此篇纯是搬运记载.. 一.测试需求描述 1. 本次测试的接口为http服务端接口 2. 接口的 ...
- [Excel] 一些实用的函数式子
这次是用php写了一个系统 收集信息,需要身份证号作为验证,但是为了信息安全 只能在数据库里面放身份证后六位.也就是说.最终导出的Excel也是只有身份证的后六位.,, 后来我发现我本地保存的完整身份 ...
- next路由跳转监听
next的路由跳转监听事件 { “routeChangeStart”, "beforeHisroryChange" "routeChangeComplete", ...
- shell批量创建数据表的一个方法
#!/bin/bash #批量新建数据表 #删除`符号,具体原因我也没搞懂 for i in {1..30};do mysql 地址 -u账号 -p密码 -e "use 库名;CREATE ...
- 关于Element UI中select组件中遇到的问题
问题一:在使用select组件设置多选功能后,窗口抖动问题? 详细描述:在我使用select做多选的功能时出现了窗口抖动的问题,测试的时候发现,在有些人的电脑上抖动,有些人的电脑上不抖,找了很多文档, ...
- c# pcm
using System; using System.IO; using System.Text; using System.Windows.Forms; using System.Runtime.I ...