Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42226 | Accepted: 20563 |
Description
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
/*
要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列。
这一个细节要考虑好该如何处理。
写这题可能会因为写成visited[i][j]来判断是否重复 这样的话就不能很好的处理这个情况
应该用一维的 visited[j]来判断 因为是按行进行dfs 如果某行的某一列
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
char mp[10][10];
int n,k;
int ans=0;
bool visited[10];
void dfs(int row,int cnt)//定义当前要dfs的行号 和 已经放了几个棋子;
{
if(cnt==k)
{
ans++;
return ;
}
if(row>=n) return ;//防止越界
for(int j=0;j<n;j++)
{
if(!visited[j]&&mp[row][j]=='#')
{
visited[j]=true; //标记
dfs(row+1,cnt+1);
visited[j]=false; //回溯
}
}
dfs(row+1,cnt);//这是一个值得注意的地方。
//当k<n时候 可能没有遍历完全部行的时候cnt就已经达到了k 所以为了处理这种情况
//当前行不放棋子 在下一行放棋子
return ;
}
/*
写法二:基本原理是一样的 相对更加的直观 可以避免考虑很多细节的坑。
dfs(-1,0);
void dfs(int row,int num)
{
if(num==k)
{
ans++;
return ;
}
for(int i=row+1; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(mp[i][j]=='#'&&!visited[j])
{
visited[j]=true;
dfs(i,num+1);
visited[j]=false;
}
}
}
return ;
}
*/
int main(void)
{
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==-1&&k==-1) break;
ans=0;
memset(visited,false,sizeof(visited));
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
dfs(0,0);
printf("%d\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31463 | Accepted: 12102 |
Description
Is an escape possible? If yes, how long will it take?
Input
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
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!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
/*
题意:三维的空间中L,R,C;起点为S 终点为E
It takes one minute to move one unit north, south, east, west, up or down
need to find the quickest way out!
在不同L维的图中,相同R和C坐标处是可以穿梭的
最短路问题直接用BFS即可。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
using namespace std;
int L,R,C;
struct node
{int x,y,z,step;
}S,E;
int flag=0,ans=0;
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
char mp[50][50][50];
bool visited[50][50][50];
bool check(node a)
{
if(a.x>=0&&a.x<L&&a.y>=0&&a.y<R&&a.z>=0&&a.z<C) return true;
return false;
}
int bfs(node start,node end)
{
queue<node>Q;
start.step=0;
Q.push(start);
visited[start.x][start.y][start.y]=true;
while(!Q.empty())
{
node head=Q.front();
Q.pop();
for(int i=0;i<6;i++)
{
node next;
next.x=head.x+dir[i][0];
next.y=head.y+dir[i][1];
next.z=head.z+dir[i][2];
if(check(next)&&!visited[next.x][next.y][next.z]&&mp[next.x][next.y][next.z]!='#')
{
next.step=head.step+1;
Q.push(next);
visited[next.x][next.y][next.z]=true;
}
if(next.x==end.x&&next.y==end.y&&next.z==end.z)
{
flag=1;
return next.step;
}
}
if(flag) return 0;
}
return 0;
}
int main(void)
{
while(scanf("%d%d%d",&L,&R,&C)!=EOF)
{
if(L==0&&R==0&&C==0) break;
flag=0;
memset(visited,false,sizeof(visited));
for(int i=0;i<L;i++)
{
for(int j=0;j<R;j++)
scanf("%s",mp[i][j]);
}
for(int i=0;i<L;i++)
{
for(int j=0;j<R;j++)
{
for(int k=0;k<C;k++)
{
if(mp[i][j][k]=='S')
{
S.x=i;
S.y=j;
S.z=k;
S.step=0;
}
if(mp[i][j][k]=='E')
{
E.x=i;
E.y=j;
E.z=k;
E.step=0;
}
}
}
}
ans=bfs(S,E);
if(flag) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 85814 | Accepted: 26925 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
/*
题意:给你两个数n k
从n到k最少需要多少分钟,一共有以下三种操作:
X - 1 or X + 1 or 2 × X 每个操作花费1分钟。
简单bfs。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
using namespace std;
const int maxn = 100000+100;
bool visited[maxn];
int dist[maxn];
int flag=0;
int bfs(int n,int k)
{
queue<int>Q;
Q.push(n);
dist[n]=0;
visited[n]=true;
while(!Q.empty())
{
int head=Q.front();
Q.pop();
for(int i=0;i<3;i++)
{
int next;
if(i==0) next=head-1;
else if(i==1) next=head+1;
else if(i==2) next=head*2;
if(!visited[next]&&next<=maxn&&next>=0)
{
Q.push(next);
dist[next]=dist[head]+1;
visited[next]=true;
if(next==k)
{
flag=1;
return dist[next];
}
}
}
}
return 0;
}
int main(void)
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
flag=0;
memset(visited,false,sizeof(visited));
memset(dist,0,sizeof(dist));
int ans=bfs(n,k);
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 29996 | Accepted: 12464 | Special Judge |
Description
Input
Output
Sample Input
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
/*
题意:给你一个数n 他的k次倍只包含0和1 输出这个数。
暴力dfs。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
using namespace std;
int flag=0;
void dfs(unsigned __int64 num,int n,int k)
{
if(flag) return ;
if(num%n==0)
{
printf("%I64u\n",num);
flag=1;
return ;
}
if(k==19) return;
dfs(num*10,n,k+1);
dfs(num*10+1,n,k+1);
}
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF,n)
{
flag=0;
dfs(1,n,0);
}
return 0;
}
Oil Deposits
/*
水题,DFS求联通块的入门题目。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
using namespace std;
char mp[110][110];
int n,m;
bool check(int x,int y)
{
if(0<=x&&x<n&&y>=0&&y<m)return true;
return false;
}
void dfs(int x,int y)
{
mp[x][y]='*';
for(int dx=-1;dx<=1;dx++)
{
for(int dy=-1;dy<=1;dy++)
{
int nx=x+dx,ny=y+dy;
if(check(nx,ny)&&mp[nx][ny]=='@') dfs(nx,ny);
}
}
}
int main(void)
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
int res=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]=='@')
{
dfs(i,j);
res++;
}
}
}
printf("%d\n",res);
}
return 0;
}