2020.3.9 ~ 2020.3.15 ACM训练周总结

2020.3.9~2020.3.15 每周总结


一、本周ACM学习相关内容

  1. 十分认真,仔细,严谨的学习了DFS和BFS的内容(《挑战程序设计竞赛》) —— 4小时

  2. 十分认真,仔细,严谨的学习了列如vector和queue等及其函数(也算是上课学的吧) —— 3小时

二、题数与耗时

题数不带比赛的补题的话也就大约5~6道,时长大约6小时

下面是解题报告:

POJ - 2386 Lake Counting

很简单的dfs,就是问出现一个w之后和它相连的w可以凑成一个水洼,问一共可以组成几个水洼;

按正常的套路做即可

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#define LL long long
#define _64 __int64
using namespace std;
​
char table[150][150];
int N,M;
​
void dfs(int x,int y){
    table[x][y] = '.';
    int nx,ny;
​
    for(int dx = -1;dx <= 1;dx++){
        for(int dy = -1;dy <= 1;dy++){
            nx = x+dx;
            ny = y+dy;
            if(nx >= 0 && nx < N && ny >= 0 && ny < M && table[nx][ny] == 'W'){
                dfs(nx,ny);
            }
        }
    }
}
​
int main(){
​
    cin >> N >> M;
    for(int i = 0;i < N;i++){
        for(int j = 0;j < M;j++){
            cin >> table[i][j];
        }
    }
​
​
    int ans = 0;
    for(int i = 0;i < N;i++){
        for(int j = 0;j < M;j++){
            if(table[i][j] == 'W'){
                dfs(i,j);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
​

POJ - 1979 Red and Black

和上一题差不多,不一样就不一样在这个计数是在dfs里面计数的

代码:

扫描二维码关注公众号,回复: 9864176 查看本文章
//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#define LL long long
#define _64 __int64
using namespace std;
​
int W,H;
char table[30][30];
int a[4] = {0,0,1,-1};
int b[4] = {1,-1,0,0};
int vis[30][30];
int ans;
​
void dfs(int x,int y){
​
    vis[x][y] = 1;
​
    for(int i = 0;i < 4;i++){
            int nx,ny;
            nx = x + b[i];
            ny = y + a[i];
            if(nx >= 0 && nx < H && ny >= 0 && ny < W && table[nx][ny] == '.' && !vis[nx][ny]){
​
                dfs(nx,ny);
                ans++;
        }
    }
​
​
}
​
int main(){
​
    while(cin >> W >> H,W != 0,H != 0){
        ans = 0;
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                cin >> table[i][j];
            }
        }
​
​
​
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                if(table[i][j] == '@' && !vis[i][j]){
                    dfs(i,j);
                    break;
                }
            }
        }
        cout << ans+1 << endl;
    }
    return 0;
}

Aizu - 0118 Property Distribution

和第一题几乎一模一样

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#define LL long long
#define _64 __int64
using namespace std;
​
int H,W;
char table[105][105];
int vis[105][105];
int a[4] = {0,0,1,-1};
int b[4] = {1,-1,0,0};
​
void dfs(int x,int y){
    vis[x][y] = 1;
​
    for(int i = 0;i < 4;i++){
        int nx = x + a[i];
        int ny = y + b[i];
        if(nx >= 0 && nx < H && ny >= 0 && ny < W && !vis[nx][ny] && table[nx][ny] == table[x][y]){
            dfs(nx,ny);
        }
    }
}
​
int main(){
    while(cin >> H >> W,H != 0,W != 0){
        int ans = 0;
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                cin >> table[i][j];
            }
        }
​
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                if(!vis[i][j]){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Aizu - 0033 Ball

题意是指一串一共十个球,每个球编号不同,十个球按照不同的顺序放入管子中,管子底端有一个挡板,可以控制球落入不同的两个管子里,问要使每个管子里的球最后都达到大的在小的上面,是否可以做到

emmmm说实话 我没用搜索,直接判断也可以嘛,,

代码:

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#define LL long long
#define _64 __int64
using namespace std;
​
int a[20];
​
int main(){
    int t;
    cin >> t;
    while(t--){
        int flag = 1;
        int ball1,ball2 = -1,ball3 = -1;
        for(int i = 0;i < 10;i++){
            cin >> ball1;
            if(flag == 1){
                if(ball1 > ball2){
                    ball2 = ball1;
                }else
                if(ball1 > ball3){
                    ball3 = ball1;
                }else{
                    flag = 0;
                }
            }
        }
​
        if(flag == 0){
            cout << "NO" << endl;
        }else{
            cout << "YES" << endl;
        }
​
​
    }
}
​

Aizu - 0558 Cheese

这是道BFS,,做了很久,,还是在队友的拉扯下才ac,代码确实很麻烦

翻译:

今年JOI镇乳酪厂也开始生产奶酪,老鼠走出窝了。JOI町被划分成东西南北,各区是巢、芝士厂、障碍物或空地。老鼠从窝里出来,到所有的奶酪工厂,每人吃一个奶酪。

这个镇上,有N个奶酪厂,每个厂都只生产一种奶酪。奶酪的硬度因工厂而异,硬度从1到N的奶酪生产厂家正好各有一个。

老鼠的第一个体力是一个,每吃一个奶酪就增加一个体力。不过,老鼠吃不到比自己体力还硬的奶酪。

老鼠,一分钟就能移动到东西南北相邻的地段,不过不可能进入障碍物区。奶酪工厂不吃奶酪也能路过。写一个寻找最短时间吃完所有奶酪的程序。不过,老鼠吃奶酪的时间是可以忽视的。

输入

输入为H+1行。第一行中3个整数H, W, N(1≤H≤1000,1≤W≤1000,1≤N≤9)按照这个顺序被写在上面。从第2行到第H+1行的各行为,'S', '1', '2',…写有由'9'、'X'、'.'构成的W字符的字符串,各个写部分表示各个划分的状态。北起i第二,从西j号的区划(i, j)和记述决定(1那些i那些h, 1那些j那些w),第i + 1行j号的文字,区划(i, j)如果窝' s ',障碍物时是' x ',如果是空地' . ' ',硬度1,2,…9是生产奶酪的工厂,分别为'1','2',…成为'9'。输入与巢和硬度1,2,…N奶酪生产厂各有一个。其他格子可以保证是障碍物或空地。老鼠可以吃到所有的奶酪。

输出

在吃完所有奶酪之前,要输出表示最短时间(分钟)的整数。 (以上转载至csdn)

在main函数里需要确定每次移动的起点和终点,每次,因为这个思路是把从一个厂子到另一个厂子的过程看作一个过程,所以每次都需要确定每次的起点和重点;

这里用到了queue和pair<int,int>(讲真我一开始根本不知道pair是干嘛的)

代码(有借鉴队(da)友(tui)的):

//去吧马里奥!把AC公主救回来!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<queue>
#define LL long long
#define _64 __int64
using namespace std;
​
int H,W,N;
char table[1005][1005];
char ts[1005][1005];
int step[1005][1005];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
​
int bfs(int sx,int sy,int gx,int gy){
    queue<pair<int,int>> que;
    for(int i = 0;i < H;i++){
        for(int j = 0;j < W;j++){
            step[i][j] = 0;
        }
    }
​
    que.push(pair<int,int>(sx,sy));
    step[sx][sy] = 1;
​
    while(que.size()){
        pair<int,int> p = que.front();
        que.pop();
        if(p.first == gx && p.second == gy){
            break;
        }
        for(int i=0;i<4;i++){
            int nx = p.first + dx[i];
            int ny = p.second + dy[i];
            if(0 <= nx && nx < H && 0 <= ny && ny < W && table[nx][ny] != 'X' && step[nx][ny] == 0){
                step[nx][ny]=1;
                que.push(pair<int,int>(nx,ny));
​
                step[nx][ny]=step[p.first][p.second]+1;
            }
​
        }
​
    }
    return step[gx][gy];
}
​
int main(){
    cin >> H >> W >> N;
    int sum = 0;
    int k=1,sx=0,sy=0,gx=0,gy=0;
    for(int i = 0;i < H;i++){
        for(int j = 0;j < W;j++){
            cin >> table[i][j];
            ts[i][j] = table[i][j];
​
            if(table[i][j] == 'S'){
                sx = i;
                sy = j;
            }
        }
    }
​
    while(k <= N){
        for(int i = 0;i < H;i++){
            for(int j = 0;j < W;j++){
                if(ts[i][j] - '0' == k){
                    gx = i;
                    gy = j;
                    table[i][j] = '.';
                }
            }
        }
        sum += bfs(sx,sy,gx,gy);
        sx = gx;
        sy = gy;
        k++;
    }
​
    cout << sum - N << endl;
}
​

以上,就这点题(哭)

三、比赛情况

3.14的比赛有事请假了,详见补题报告

四、锻炼情况

214斤,正朝着210迈进(难啊)

猜你喜欢

转载自www.cnblogs.com/CCCCrack/p/12503501.html
今日推荐