SGU 125 Shtirlits 搜索+可行性剪枝
500ms时限406ms水过……
直接枚举肯定超时,需要剪枝。
枚举每个格子的元素,检查其左上角和正上方格子是否满足条件,若不满足不必再向下搜索。
在 这里 看到一个更好的方法: 枚举每个格子是哪个相邻的比它大。然后DFS看看有没有环。这样的复杂度只有(2^5*3^5)。
不过我没写出来……而且也不太清楚这个时间复杂度是怎么算的……求指点!
#include <cstdio>
#include <cstring>
#include <cstdlib> const int dx[] = { -, , , };
const int dy[] = { , , -, }; int N;
int mat[][];
int G[][]; bool check( int i, int j )
{
return i >= && i < N && j >= && j < N;
} //wh=true 代表检查左上角格子是否满足条件, wh=false代表检查正上方格子
bool ok( int x, int y, bool wh )
{
int cnt = ;
for ( int k = ; k < ; ++k )
{
int xx = x + dx[k];
int yy = y + dy[k];
if ( check( xx, yy ) && G[xx][yy] > G[x][y] ) ++cnt;
} if ( wh ) return cnt == mat[x][y];
return cnt <= mat[x][y];
} bool Judge()
{
int cnt = ; for ( int i = ; i < N; ++i )
{
for ( int j = ; j < N; ++j )
{
cnt = ;
for ( int k = ; k < ; ++k )
{
int xx = i + dx[k];
int yy = j + dy[k];
if ( check( xx, yy ) )
{
if ( G[xx][yy] > G[i][j] )
++cnt;
}
}
if ( cnt != mat[i][j] ) return false;
}
} return true;
} bool DFS( int cur )
{
if ( cur == N * N )
{
if ( Judge() ) return true;
return false;
} int x = cur / N;
int y = cur % N;
for ( int i = ; i < ; ++i )
{
G[x][y] = i; bool okey = true; if ( check( x - , y - ) )
{
if ( !ok( x - , y - , true ) )
okey = false;
} if ( okey && check( x - , y ) )
{
if ( !ok( x - , y, false ) )
okey = false;
} if ( okey )
{
if ( DFS( cur + ) )
return true;
} G[x][y] = -;
} return false;
} int main()
{
//freopen( "s.out", "w", stdout );
while ( scanf( "%d", &N ) == )
{
for( int i = ; i < N; ++i )
for( int j = ; j < N; ++j )
scanf( "%d", &mat[i][j] ); memset( G, -, sizeof( G ) );
if ( DFS( ) )
{
for ( int i = ; i < N; ++i )
{
for ( int j = ; j < N; ++j )
{
if ( j ) putchar(' ');
printf( "%d", G[i][j] );
}
puts("");
}
}
else puts("NO SOLUTION");
}
return ;
}
SGU 125 Shtirlits 搜索+可行性剪枝的更多相关文章
- sgu 125 Shtirlits dfs 难度:0
125. Shtirlits time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a checkered fi ...
- Shtirlits - SGU 125(搜索)
题目大意:B[i, j]表示周围有多少个比它大的数,能否用B数组构造出一个A数组,如果不能输出“NO SOLUTION”. 分析:数据规模比较小,可以直接暴力枚举每个点的值. 代码如下: #inclu ...
- SGU 125.Shtirlits
时间限制:0.25s 空间限制:4M 题意: 有N*N的矩阵(n<=3),对所有i,j<=n有G[i][j]<=9,定义f[i][j]为G[i][j]四周大于它的数的个数(F[i][ ...
- 深搜的剪枝技巧(三)——Sticks(可行性剪枝、上下界剪枝、最优性剪枝)
小木棍(最优性剪枝.可行性剪枝) 一.问题描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,已知每段的长都不超过 50 .现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...
- luogu P1526 [NOI2003]智破连环阵 搜索+最大匹配+剪枝
LINK:智破连环阵 考试的时候 题意没理解清楚 题目是指一个炸弹爆炸时间结束后再放另一个炸弹 而放完一个炸弹紧接另一个炸弹.题目中存在然后二字. 这样我们可以发现某个炸弹只会炸连续的一段. 但是 由 ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
- [CF293B]Distinct Paths_搜索_剪枝
Distinct Paths 题目链接:http://codeforces.com/problemset/problem/293/B 数据范围:略. 题解: 带搜索的剪枝.... 想不到吧..... ...
- ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)
题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...
- 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)
主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...
随机推荐
- 项目中用到的js日期函数
<script type="text/javascript"> //替换字符串 function Replace(str, from, to) { ...
- ORM 框架
1.Dapper 2.Entity Framework(EF) http://www.cnblogs.com/n-pei/archive/2011/09/06/2168433.html
- python 单元测试-unittest
参考资料:https://docs.python.org/3.4/library/unittest.html#module-unittest 一张图解决问题: 涉及5块内容:case.suite.lo ...
- transitionend 事件的兼容
google :webkitTransitionEnd firefox :transitionend ie : MSTransitionEnd
- UIlabel 显示模糊
问题: 今天遇到连续两个label一个显示的比较清楚,比较锐利,而另一个对比下有点模糊. 原因: 在使用UILabel等继承于UIView的控件时,如果frame的rect不是整数的情况下,就会显示起 ...
- IOS 8 关于 Touch ID
一.什么是Touch ID? Touch ID是在iPhone 5s后的设备上出现的指纹识别.Apple在IOS 8中开放给第三方APP使用. 可以使用 Touch ID 来验证用户的身份,用户经验证 ...
- 小技巧---查doc文档的index.html怎么用的和chm一样
看包里面是否有E:\Java\hibernate3.3.2\hibernate-annotations-3.4.0.GA\hibernate-annotations-3.4.0.GA\doc\refe ...
- eclipse中设置中文javadoc+如何查看class的中文javadoc
一. eclipse中设置中文javadoc 1.先到http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish ...
- c++new和new()区别(了解)
我们在C++程序中经常看到两种new的使用方式:new A以及new A().那么这两种究竟有什么区别呢? 调用new分配的内存有时候会被初始化,而有时候不会,这依赖于A的类型是否是POD(Plain ...
- JavaScript之substring()方法讲解
定义和用法 substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数 描述 start 必需.一个非负 ...