标签:led 移动 ++ ted after max alt font strong
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36385 | Accepted: 17950 |
Description
Input
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
【分析】:建议学习回溯的时候对N皇后还有N皇后的变式好好学习一下,类N皇后真是学习回溯非常好的例题。
如在第i行第j列,遇到‘#‘号。那么接下来的处理就有两种情况了。
第一种:把i,j放入到一个数组C中,然后继续向第i+1行进行搜索,直到找到m个位置或者到了棋盘的边界
另一种:不选择第i行第j列的位置,然后继续向第i+1行进行搜索,直到找到m个位置或者到了棋盘的边界
【代码】:
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <set>
#include <map>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
#pragma comment(linker, "/STACK:102400000,102400000")
#define Abs(x) ((x^(x >> 31))-(x>>31))
#define Swap(a,b) (a^=b,b^=a,a^=b)
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define MOD 1000000007
#define max_ 505
#define maxn 200002
using namespace std;
int n,m;
char s[10][10];//表示棋盘
int c[10];//表示每一列有没有摆放过棋子
int tot,cnt;
void dfs(int cur)//cur表示当前所在行
{
if(cnt == m)//cnt表示当前所摆放棋子数目
{
tot++;
return ;
}
if(cur >= n)//超出搜索范围
return ;
for(int j=0;j<n;j++)
{
if(!c[j] && s[cur][j]==‘#‘)//空白处并且还没有摆放棋子
{
c[j]=1;
cnt++;
dfs(cur+1);//搜索下一行
c[j]=0;//标记清除
cnt--;
}
}
dfs(cur+1);//如果当前行没有可以摆放的位置 或者cnt已经等于m 但是还没有搜索完整个棋盘 将要继续搜索下一行
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==-1&&m==-1) break;
memset(c,0,sizeof(c));//将标记初始化为0
tot=cnt=0;
for(int i=0;i<n;i++)
{
scanf("%s",&s[i]);
}
dfs(0);
printf("%d\n",tot);
}
}
POJ - 2251 Dungeon Master 【三维dfs】
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Escaped in 11 minute(s).
Trapped!
【题意】:给出一三维空间的地牢,要求求出由字符‘S‘到字符‘E‘的最短路径
移动方向可以是上,下,左,右,前,后,六个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的
【代码】:
标签:led 移动 ++ ted after max alt font strong
原文地址:http://www.cnblogs.com/Roni-i/p/7798527.html