ACM/ICPC 之 枚举(POJ1681-画家问题+POJ1166-拨钟问题+POJ1054-讨厌的青蛙)
POJ1681-画家问题
枚举的经典例题,枚举第一行即可,其余行唯一。
//画家问题,y表示黄色,w表示白色,怎样让墙上所有方格为y,操作类似熄灯问题poj1222
//memory 136K Time: 297 Ms
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std; #define INF 0x3f3f3f3f
#define MAX 20 char m[MAX][MAX];
int n,ans,sum;
int map[MAX][MAX],cpmap[MAX][MAX],p[MAX];
int move[][] = {{,},{-,},{,},{,-}}; void paint(int x,int y)
{
sum++;
cpmap[x][y] = !cpmap[x][y];
for(int i=;i<;i++)
{
int tx = x+move[i][];
int ty = y+move[i][];
if(tx>= && tx<n && ty>= && ty<n)
cpmap[tx][ty] ^= ;
}
} /*尝试绘画*/
int test_paint()
{
int i,j;
for(i=;i<n;i++) //第一行
if(p[i])
paint(,i);
for(i=;i<n;i++) //其余行唯一
{
for(j=;j<n;j++)
{
if(!cpmap[i-][j])
paint(i,j);
}
}
//判断最后一行是否符合
int flag = ;
for(i=;i<n;i++)
if(!cpmap[n-][i])
{
flag = ;break;
}
return flag;
} int main()
{
int T;
int i,j;
scanf("%d",&T);
while(T--)
{
memset(p,,sizeof(p));
ans = INF;
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%s",m[i]);
//字符-转换为-0与1
for(i=;i<n;i++)
for(j=;j<n;j++)
if(m[i][j] == 'y')
map[i][j] = ;
else
map[i][j] = ; p[] = -;
int res;
while()
{
sum = ;
memcpy(cpmap,map,sizeof(map));
/*二进制枚举第一行所有画法*/
p[]++;
res = p[]/;
j=;
while(res)
{
p[j-] = ;
p[j]++;
res = p[j++]/;
}
if(p[n])
break;
if(test_paint())
if(ans > sum)
ans = sum;
}
if(ans == INF)
printf("inf\n");
else
printf("%d\n",ans);
}
return ;
}
POJ1166-拨钟问题
分析后枚举所有可能情况。
//暴力枚举-熄灯问题变形-拨钟问题,给出九个钟的时针位置(3,6,9,12)-(1,2,3,0)-将他们调到12点 即
//Memory 134K Time: 0 Ms #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; int sclock[]; //source-clock
int pc[]; //调好后时针位置 int main()
{
int j;
for (j = ; j <= ; j++)
scanf("%d",&sclock[j]);
int i[]; //枚举所有九种拨法的情况-每种拨法最多3次 (第四次相当于没有拨动)
//4^9种情况
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
for (i[] = ; i[] < ; i[]++)
{
pc[] = (sclock[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[] + i[]) % ;
pc[] = (sclock[] + i[] + i[] + i[]) % ; int sum = ;
for (j = ; j <= ; j++)
sum += pc[j];
if (!sum)
{
for (j = ; j <= ;j++)
while (i[j]--)
printf("%d ",j);
printf("\n");
return ;
}
}
return ;
}
POJ1054-讨厌的青蛙
//Flog直线等步长踩稻子(array),找出踩过最多的一条Flog路径
//Memory 172K Time: 47 Ms
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std; #define min(x,y) ((x)>(y)?(y):(x))
#define MAX 5005 struct Node {
int x, y;
}map[MAX]; //踩过路径 int row, col, length;
int n, maxlen = ; bool operator < (const Node &a, const Node &b) //重载全局运算符 <,以满足binary_search()与sort()处理Node的需要
{
//二重排序-行列序
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
} /*延长路径-判定该路径是否成立*/
void extend(Node second, int cx, int cy)
{
Node t;
t.x = second.x + cx;
t.y = second.y + cy;
while (t.x >= && t.x <= row && t.y >= && t.y <= col)
{
if (!binary_search(map, map + n, t))//没有find-不成立
{
length = ;
break;
}
//find-成立
length++;
t.x += cx;
t.y += cy;
}
if (length > maxlen)
maxlen = length;
} int main()
{
scanf("%d%d%d", &row, &col, &n);
for (int i = ; i<n; i++)
scanf("%d%d", &map[i].x, &map[i].y); sort(map, map + n);
for (int i = ; i < n; i++)
for (int j = i + ; j < n; j++)
{
length = ;
int cx = map[j].x - map[i].x; //横步长
int cy = map[j].y - map[i].y; //纵步长
int px = map[i].x - cx; //(px,py)为假想跳跃起始位置
int py = map[i].y - cy; if (px >= && px <= row && py >= && py <= col)
continue; //已访问此路径或路径不存在 px = map[i].x + (maxlen - )*cx;
py = map[i].y + (maxlen - )*cy;
if (px > row) break; //纵向须跳跃的最小距离超过row-遍历第一跳跃点
if (py > col || py < ) continue; //横向须跳跃的最小距离超过col-遍历第二跳跃点
extend(map[j], cx, cy); //极值无误-尝试拓展
} if (maxlen == ) maxlen = ; //没有最长路径
printf("%d\n", maxlen);
return ;
}
ACM/ICPC 之 枚举(POJ1681-画家问题+POJ1166-拨钟问题+POJ1054-讨厌的青蛙)的更多相关文章
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time
Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- 2016 ACM/ICPC Asia Regional Qingdao Online(2016ACM青岛网络赛部分题解)
2016 ACM/ICPC Asia Regional Qingdao Online(部分题解) 5878---I Count Two Three http://acm.hdu.edu.cn/show ...
- [C++]最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583)
Question 例题3-5 最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583) 如果x+x的各个数字之和得到y,就是说x是y的生成元.给出n( ...
- ACM ICPC Kharagpur Regional 2017
ACM ICPC Kharagpur Regional 2017 A - Science Fair 题目描述:给定一个有\(n\)个点,\(m\)条无向边的图,其中某两个点记为\(S, T\),另外标 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
- Codeforces Round #445 A. ACM ICPC【暴力】
A. ACM ICPC time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
随机推荐
- 说说C#的async和await(转)
一个简单的例子: public class MyClass { public MyClass() { DisplayValue(); //这里不会阻塞 System.Diagnostics.Debug ...
- git执行pull命令时,报错
在图形界面中,执行拉取操作时,出现下面的错误. You asked to pull from the remote 'origin', but did not specifya branch. Bec ...
- Java通过JNI调用C
Java调用C有多种方式,本文介绍笔者最近的学习过程,避免今后再犯类似的错误. 首先,Java肯定是调用C的动态链接库,即通过编译器编译后的dll/so文件. 下面介绍gcc编译dll的方法. 一般情 ...
- PHP+MySQL存储数据出现中文乱码的问题
PHP+MySQL出现中文乱码的原因: 1. MYSQL数据库的编码是utf8,与PHP网页的编码格式不一致,就会造成MYSQL中的中文乱码. 2. 使用MYSQL中创建表.或者选择字段时设置的类型不 ...
- 11个很棒的 jQuery 图表库
如果你曾经使用过任何类型的数据,你应该知道阅读一排排数据的痛苦.通过所有这些数据弄清楚他们的意思是非常不容易的.可视化对于解决这个问题起到了重要的作用.可视化降低了数据阅读的难度,帮助决策者获得可操作 ...
- [译]Node.js : Building RESTful APIs using Loopback and MySQL
国庆后可能就要使用StrongLoop那套东西来做项目了 原文:http://www.javabeat.net/loopback-mysql/ Loopback是什么? Loopback是一个开源的N ...
- XDU 1160 - 科协的数字游戏I
Problem 1160 - 科协的数字游戏I Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 184 ...
- solrcloud
@Test public void querySolrCloud(){ String zkHost = "127.0.0.1:2181"; String defaultCollec ...
- informatica通用命令方式启动workflow
#传mapping参数时#调用执行workflow,-sv informatica服务,-d 域 -u 用户,-p 密码 -folder知识库下的workflow所在文件夹,-wait wf_test ...
- phpcms万能字段如何使用php方法
来自:http://www.tantengvip.com/2013/12/phpcms-php-function/ phpcms后台内容模块->模型管理->添加字段功能很强大,你只需在ph ...