2021-01-28学习总结
上午8:00 学习打卡
上午9:00 – 11:00 (2h)
学习高数,宋浩老师讲课真的很好而且很有趣。
下午13:00 – 17:00(4h)
继续做VJ平台的题,今天才被告知谷歌浏览器自带翻译,妈妈再也不用担心我会看不懂洋文题了,芜湖~
上题
H - Red and Black
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
…#.
…#
…
…
…
…
…
#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.
…
11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13
题意:
求出与’@’四个方向相邻的所有 ’ . ’ 的个数。
思路:
利用bfs去找所有符合条件的点。
一开始我的思路是对的,但是还是总是怎么敲怎么都过不了样例,于是乎,我就有去看了《啊哈算法》,啊哈算法P95里有个再解炸弹人,我瞅着它输入的数据跟样例的数据有点像,我就试着魔改了一下,发现诶,真的可了,但是还是有点懵懵的,我打算再去琢磨琢磨。
AC代码
#include<stdio.h>
#include<string.h>
struct note
{
int x;
int y;
};
int main()
{
struct note que[410];
int head,tail;
char a[50][50];
int book[50][50];
int i,j,k,sum,mx,my,n,m,tx,ty,startx,starty;
int next[4][2]={{0,1},//向右走
{1,0},//向下走
{0,-1},//向左走
{-1,0}};//向上走;
while(scanf("%d%d",&m,&n))
{
if(n==0&&m==0)
break;
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]=='@')
{
startx=i;
starty=j;
}
sum=0;
memset(book,0,sizeof(book));
head=1;
tail=1;
que[tail].x=startx;
que[tail].y=starty;
tail++;
book[startx][starty]=1;
sum=1;
while(head<tail)
{
for(k=0;k<4;k++)
{
tx=que[head].x+next[k][0];
ty=que[head].y+next[k][1];
if(tx<0||tx>n-1||ty<0||ty>m-1)
continue;
if(a[tx][ty]=='.'&&book[tx][ty]==0)
{
sum++;
book[tx][ty]=1;//标记这个点已经走过。
que[tail].x=tx;
que[tail].y=ty;
tail++;
}
}
head++;
}
printf("%d\n",sum);
}
}
敲完这题我发现二叉树都没动过,就又跑去敲二叉树了。敲了一题水题,就不贴上来了。
晚上 18:30 – 21:30(3h)
写星火英语四级卷,继续学习二叉树;
总计9h