一、本周ACM学习相关内容
师哥安排了12道题,做了五道(不包含比赛题);大概4个小时;
题解报告:
很简单的dfs,遍历数组,找到w就dfs周围的八个点,直到递归结束;
#include
#include
#include
#include
using namespace std;
int m,n;
char a[1001][1001];
void DFS(int i,int j)
{
if(i>=0&&i
a[i][j] = '.';
DFS(i-1,j);DFS(i-1,j-1);DFS(i-1,j+1);DFS(i,j-1);DFS(i,j+1);DFS(i+1,j-1);DFS(i+1,j);DFS(i+1,j+1);}
}
int main()
{
int i,j,sum = 0;
cin>>n>>m;
memset(a,'0',sizeof(a));
for(i = 0;i<n;i++)
scanf("%s",a[i]);
for(i = 0;i<n;i++)
for(j = 0;j<m;j++)
if(a[i][j]=='W')
{
sum++;
DFS(i,j);
}
printf("%d\n",sum);
}
与上一题类似,不同的是递归到周围四个点即可,并且计数要在函数内
#include
#include
#include
using namespace std;
int w,h,i,j;
int step;
int sx,sy;
char a[21][21];
void dfs(int x,int y)
{
if(x>=0&&x
{
a[x][y] = '#';
step++;
dfs(x-1,y);dfs(x+1,y);dfs(x,y+1);dfs(x,y-1);
}
}
int main()
{
while(cin>>w>>h&&w!=0&&h!=0){
int flag = 0;
step = 0;
for( i = 0;i<h;i++)
scanf("%s",a[i]);
for( i = 0;i<h;i++){
for( j = 0;j<w;j++)
{
if(a[i][j]=='@')
{
a[i][j] = '.';
sx = i;
sy = j;
flag = 1;
break;
}
}if(flag == 1)
break;
}
dfs(sx,sy);
cout<<step<<endl;
}
}
与第一题一样
#include
#include
#include
using namespace std;
int w,h;
char a[101][101];
int res;
void dfs(int x,int y,char s)
{
char t;
if(x>=0&&x
t = a[x][y];
a[x][y] = 'O';
dfs(x+1,y,t);dfs(x-1,y,t);dfs(x,y+1,t);dfs(x,y-1,t);}
}
int main()
{
while(cin>>h>>w&&h!=0&&w!=0)
{int i,j;
res = 0;
for(i = 0;i<h;i++)
scanf("%s",a[i]);
for(i = 0;i < h;i++)
for(j = 0;j<w;j++)
{
if(a[i][j]!='O')
{
res++;dfs(i,j,a[i][j]);
}
}
cout<<res<<endl;
}
}
暴力模拟即可,没有难度。。。。(严重怀疑是师哥为了给我们增长信心加的题)
#include
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
vector
vector
while(n--)
{
int flag = 0;
int i;
int num[10];
for(i = 0;i<=9;i++)
cin>>num[i];
a.push_back(0);
b.push_back(0);
for(i = 0;i<10;i++)
{
if(num[i]>a.back()||num[i]>b.back())
{
if(num[i]>a.back()&&num[i]<b.back())
{
a.push_back(num[i]);
}
else if(num[i]>b.back()&&num[i]<a.back())
{
b.push_back(num[i]);
}
else if(num[i]>a.back()&&num[i]>b.back())
{
if(a.back()>b.back())
a.push_back(num[i]);
else
b.push_back(num[i]);
}
if(i==9)
flag = 1;
}
else{
flag = 0;break;
}
}
if(flag == 1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
a.clear();b.clear();
}
}
说实话,这个题才体现出bfs的魅力和难懂性。。。。。。感谢张师姐
#include
#include
#include
using namespace std;
struct node{
int x,y;
int step;
node(){
step = 0;
}
};
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
char mp[1005][1005];//迷宫
int vis[1005][1005];//访问状态
int sx,sy;//起始位置;
int main()
{
int h,w,n;
cin>>h>>w>>n;//输入
for(int i = 0;i < h;i++)
{
for(int j = 0;j < w;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='S')
{
sx = i;sy = j;
}
}
}//生成地图并寻找起始点
int ans = 0;//声明结果
queue
for(int i = 1;i <= n;i++)
{
memset(vis, 0, sizeof(vis));//重置访问状态;
node now,next;
now.x = sx;
now.y = sy;
vis[sx][sy] = 1;
q.push(now);
while(q.empty()==false)
{
now = q.front();
q.pop();
if(mp[now.x][now.y] == '0'+i)
{
sx = now.x;
sy = now.y;
ans = ans+now.step;
break;
}//吃到了
for(int j = 0;j < 4;j++)
{
int xx = now.x+dx[j];
int yy = now.y+dy[j];
if(xx>=0&&xx
{
vis[xx][yy] = 1;
next.x = xx;
next.y = yy;
next.step = now.step+1;
q.push(next);
}
}
}
while(!q.empty())
q.pop();
}
cout<<ans<<endl;
}
3.14的比赛有事请假了
体育作业:每周1200米
手机扫一扫
移动阅读更方便
你可能感兴趣的文章