hdoj1241 Oil Deposits
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 67090 Accepted Submission(s): 38717
Problem DescriptionThe GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.InputThe input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.OutputFor each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.Sample Input1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0Sample Output
0122
怎么可以这么笨!快哭了 才刚开始深搜就理解这么费劲了..
题意:输入行列数和字符 @表示油田 *表示空地 上下左右以及斜对角连通都表示同一块油田 输出油田数
ac代码:
#include<iostream>
char oil[105][105];//记录总地图
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,1},{1,0}};
int m,n;
void dfs(int x,int y)
{
//遍历周围八个点
for(int i=0;i<8;i++)
{
int x1=x+dir[i][0];
int y1=y+dir[i][1];
if(x1>=0&&x1<m&&y1>=0&&y1<n&&oil[x1][y1]=='@')
{
oil[x1][y1]='*';
dfs(x1,y1);
}
}
}
int main()
{
using namespace std;
while(cin>>m>>n,m)
{
int num=0;
for(int i=0;i<m;++i)
cin>>oil[i];
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
{
if(oil[i][j]=='@')
{
oil[i][j]='*';
num++;
dfs(i,j);
}
}
cout<<num<<endl;
}
}
那个遍历的过程及dir[x][y]如果看不懂的话也可以写成这样(我才不说是谁理解了半天还没懂哭泣
dfs(x-1,y-1);
dfs(x-1,y);
dfs(x-1,y+1);
dfs(x,y-1);
dfs(x,y+1);
dfs(x+1,y-1);
dfs(x+1;y+1);
dfs(x+1;y);
就是深搜周围八个点 表达意思一样啦
每经过一个油田可以把它变成空地表示自己访问过了 或者可以用一个visit[x][y]数组记录自己有没有访问过这个地方
acwing842 排列数字
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N=10;
int path[N];//保存路径
bool st[N];//记录当前点有没有被输出过
void dfs(int u,int n)
{
if(u==n)//如果找到最后一层了
{
for(int i=0;i<n;++i)
cout<<path[i]<<' ';
cout<<endl;
return;
}
for(int i=1;i<=n;++i)
{
if(!st[i])//如果当前数字i没有被用过
{
path[u]=i;//保存数字i到路径中
st[i]=true;//当前数字被用过了
dfs(u+1,n);//继续往下深搜
st[i]=false;//回溯 恢复原样
}
}
}
int main()
{
int n;
cin>>n;
dfs(0,n);
return 0;
}
acwing843 n皇后问题
代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int N=20;//因为有对角线 所以开20
char g[N][N];
bool col[N]={false},dg[N]={false},udg[N]={false};
int n;
void dfs(int u)//以行搜索
{
if(u==n)//当深搜行数等于总行数 输出每一行
{
for(int i=0;i<n;++i)
puts(g[i]);//输出字符不会自动换行
puts("");//puts函数输出字符串会自动换行
return;
}
for(int i=0;i<n;++i)
{
if(!col[i]&&!dg[u+i]&&!udg[i-u+n])//这里dg[]和udg[]里的下标都是用截距表示该对角线
{
g[u][i]='Q';
col[i]=dg[u+i]=udg[i-u+n]=true;
dfs(u+1);
col[i]=dg[u+i]=udg[i-u+n]=false;
g[u][i]='.';
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
g[i][j]='.';
dfs(0);
return 0;
}
杭电有道类似的题 只不过是输出排列个数 hdoj2553 记得打表不然会TLE