一、本周ACM学习相关内容
- 学习了dfs和bfs —— 4小时
- 课上系统的学习了vector等stl函数的使用即注意事项—3小时
二、题数与耗时
师哥安排了12道题,做了五道(不包含比赛题);大概4个小时;
题解报告:
POJ - 2386 Lake Counting
很简单的dfs,遍历数组,找到w就dfs周围的八个点,直到递归结束;
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int m,n;
char a[1001][1001];
void DFS(int i,int j)
{
if(i>=0&&i<n&&j>=0&&j<m&&a[i][j]=='W'){
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);
}
POJ - 1979 Red and Black
与上一题类似,不同的是递归到周围四个点即可,并且计数要在函数内
#include<iostream>
#include<cstdio>
#include<algorithm>
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<h&&y>=0&&y<w&&a[x][y]=='.')
{
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;
}
}
Aizu - 0118 Property Distribution
与第一题一样
#include<iostream>
#include<string>
#include<cstdio>
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<h&&y>=0&&y<w&&a[x][y]==s){
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;
}
}
Aizu - 0033 Ball
暴力模拟即可,没有难度。。。。(严重怀疑是师哥为了给我们增长信心加的题)
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int >a;
vector<int >b;
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();
}
}
Aizu - 0558 Cheese
说实话,这个题才体现出bfs的魅力和难懂性。。。。。。感谢张师姐
#include<iostream>
#include<queue>
#include<cstring>
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 <node> q;
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<h&&yy>=0&&yy<w&&mp[xx][yy]!='X'&&vis[xx][yy]==0)
{
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米