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 搜索+可行性剪枝的更多相关文章

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

  2. Shtirlits - SGU 125(搜索)

    题目大意:B[i, j]表示周围有多少个比它大的数,能否用B数组构造出一个A数组,如果不能输出“NO SOLUTION”. 分析:数据规模比较小,可以直接暴力枚举每个点的值. 代码如下: #inclu ...

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

  4. 深搜的剪枝技巧(三)——Sticks(可行性剪枝、上下界剪枝、最优性剪枝)

    小木棍(最优性剪枝.可行性剪枝) 一.问题描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,已知每段的长都不超过 50 .现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...

  5. luogu P1526 [NOI2003]智破连环阵 搜索+最大匹配+剪枝

    LINK:智破连环阵 考试的时候 题意没理解清楚 题目是指一个炸弹爆炸时间结束后再放另一个炸弹 而放完一个炸弹紧接另一个炸弹.题目中存在然后二字. 这样我们可以发现某个炸弹只会炸连续的一段. 但是 由 ...

  6. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

  7. [CF293B]Distinct Paths_搜索_剪枝

    Distinct Paths 题目链接:http://codeforces.com/problemset/problem/293/B 数据范围:略. 题解: 带搜索的剪枝.... 想不到吧..... ...

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

  9. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)

    主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...

随机推荐

  1. 【Valid Parentheses】cpp

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  2. 农行网银软件导致XP死机

    一台PC电脑安装了XP系统,突然会大约30分钟左右死机,键盘(CTRL+ALT+DEL)和鼠标都没反应,只能强行关机后重新开机.因为是用了几年的电脑,担心热稳定性问题,打开机箱后,发现CPU风扇很多灰 ...

  3. 面向对象版js分页

    基于前一个js分页,我将代码改成一个面向对象版的js分页,代码如下 http://peng666.github.io/blogs/pageobj <!DOCTYPE html> <h ...

  4. NSIS中判断.net framework 是否安装

    在网上找了几个脚本,原理都是读取注册表进行判断,都可以用. 1.CheckDotNetFramework /* * Name: CheckDotNetFramework.nsh * Version: ...

  5. curl Protocol 'http not supported or disabled in libcurl

    C:\Documents and Settings\ganiks.liu\Desktop\curl-7.37.0-win32\bin>curl -V curl 7.37.0 (i386-pc-w ...

  6. 代码优先-Code First

    非常有用的两篇文章 MSDN:Code First 迁移 博客园:CodeFirst数据迁移(不丢失数据库原有数据) EF有三种开发模式:Model First,Database First 和 Co ...

  7. Sqli-labs less 40

    Less-40 本关的sql语句为SELECT * FROM users WHERE id=('$id') LIMIT 0,1 我们根据sql语句构造以下的payload: http://127.0. ...

  8. 学生信息管理 --- c语言实现

    第一次写比较大的程序,昨晚看了  大话数据结构  有感-----同样求 1+2+3+4+...+100,我则是简单的从1+2+3+4+...+100. 而不是 (1+100) / 2 * 100;(高 ...

  9. awk处理之案例二:awk匹配文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  10. POJ 1496

    #include <iostream> #include <string> using namespace std; int fac(int num); int C(int n ...