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-讨厌的青蛙)的更多相关文章

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

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

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

  4. [C++]最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583)

    Question 例题3-5 最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583) 如果x+x的各个数字之和得到y,就是说x是y的生成元.给出n( ...

  5. ACM ICPC Kharagpur Regional 2017

    ACM ICPC Kharagpur Regional 2017 A - Science Fair 题目描述:给定一个有\(n\)个点,\(m\)条无向边的图,其中某两个点记为\(S, T\),另外标 ...

  6. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

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

  8. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  9. ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))

    祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...

随机推荐

  1. Linux服务器管理: 日志管理(二)

    日志的轮替 1.日志文件的命名规则 a.如果配置文件中拥有"dateext"参数,那么日志会用日期来作为日志文件的后缀,例如:"secure-20150630" ...

  2. Memcached存储命令 - set

    Memcached set 命令用于将 value(数据值) 存储在指定的 key(键) 中. 如果set的key已经存在,该命令可以更新该key所对应的原来的数据,也就是实现更新的作用. set 命 ...

  3. CF456C Boredom (DP)

    Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...

  4. AngularJs基础总结(1.4版本)

    注明:现在用的是最新的1系列1.4版本. 一.细节方面入手: 1,ng-app根节点,一般别写在html上面,最多也就写在body就行了,因为我们难免会忘记这个写在哪里,然后复制一些案例代码却总报错. ...

  5. Roslyn词法分析器初使用

    需:install-package:Microsoft.CodeAnalysis ];         NamespaceDeclarationSyntax NameSpaceDeclaration  ...

  6. [译]angularjs directive design made easy

    原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...

  7. mybaties # , $

    mybaties会对#引入的值加上双引号, 如: #{buildingName} -------->"buildingName";mybaties会将$引入的值直接显示到sq ...

  8. SQL Server 服务器器信息备份(一)--login新建脚本备份

    前言 若你的企业使用SQL Server数据库镜像为容灾技术. 那你一定做过在镜像切换之前要新建Login,而且若Login密码不同,要修改链接数据库的字符串,在切换完之后则仍需要给数据库重新赋予权限 ...

  9. Handler Should be static or leaks Occur?

    解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...

  10. jQuery中添加自定义或函数方法

    <script type="text/javascript"> (function () { $.fn.parHy = function (val) { alert(v ...