1. 程式人生 > >ACM大獎賽前恢復性訓練

ACM大獎賽前恢復性訓練

放棄了之前更新很久的NOI時期的Blog 開了此新號

也代表了和過去的自己告別 這是一個全新的開始。

本BLOG從10.19開始更新

 

DAY1 會寫程式碼

水題略去

讀入優化

鋪地毯

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int MAXN = 10000 + 10;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

int N, ex, ey;
struct data { int a, b, g, k; }map[MAXN];

int main() {
    N = read();
    for(int i = 1; i <= N; ++i) 
        map[i].a = read(), map[i].b = read(), map[i].g = read(), map[i].k = read();
    ex = read(); ey = read();
    for(int i = N; i >= 1; --i) {
        if(map[i].a <= ex && map[i].a + map[i].g >= ex && map[i].b <= ey && map[i].b + map[i].k >= ey) {
            printf("%d\n", i);
            return 0;
        }
    }
    puts("-1\n");
    return 0;
}

 

DAY2 DFS BFS等搜尋

POJ 2386

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int MAXN = 100 + 10;
const int MAXM = 100 + 10;

int N, M, cnt;
char map[MAXN][MAXM];

void dfs(int x, int y) {
    map[x][y] = '.';
    for(int dx = -1; dx <= 1; ++dx)
        for(int dy = -1; dy <= 1; ++dy) {
            int nx = x + dx, ny = y + dy;
            if(nx >= 1 && nx <= N && ny >= 0 && ny < M && map[nx][ny] == 'W') dfs(nx, ny);
        }
    return;
}

int main() {
    scanf("%d%d", &N, &M);
    for(int i = 1; i <= N; ++i) scanf("%s", map[i]);
    for(int i = 1; i <= N; ++i)
        for(int j = 0; j < M; ++j)
            if(map[i][j] == 'W') {
                dfs(i, j);
                cnt++;
            }
    printf("%d\n", cnt);
    return 0;
}

POJ 1979

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int W, H, cnt;
char map[30][30];
int dx[5] = {0, 1, 0, -1, 0};
int dy[5] = {0, 0, 1, 0, -1};

void dfs(int x, int y) {
	cnt++; map[x][y] = 'A';
	for(int i = 1; i <= 4; ++i) {
		int nx = x + dx[i], ny = y + dy[i];
		if(nx >= 1 && nx <= H && ny >= 0 && ny < W && map[nx][ny] == '.') dfs(nx, ny);
	}
	return;
}

int main() {
	while(scanf("%d%d", &W, &H) && W && H) {
		cnt = 0;
		memset(map, 0, sizeof(map));
		for(int i = 1; i <= H; ++i) scanf("%s", map[i]);
		for(int i = 1; i <= H; ++i)
			for(int j = 0; j < W; ++j)
			     if(map[i][j] == '@') dfs(i, j);
		printf("%d\n",cnt);
	}
	return 0;
}

POJ 2251 練廣搜 Trapped不能換行否則會PE MDZZ

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int dx[7] = {0, 0, 0, 0, 0, 1, -1};
const int dy[7] = {0, 0, 0, 1, -1, 0, 0};
const int dz[7] = {0, 1, -1, 0, 0, 0, 0};

char map[40][40][40];
bool vis[40][40][40];
int stx, sty, stz, edx, edy, edz, k, n, m;
struct data {
    int x, y, z;
    int step;
};

inline bool check(int x, int y, int z) {
    if(x < 0 || y < 0 || z < 0 || x >= k || y >= n || z >= m) return false;
    else if(map[x][y][z] == '#' || vis[x][y][z]) return false;
    return true;
}

inline int bfs() {
    queue<data> Q;
    data now, next;
    now.x = stx, now.y = sty, now.z = stz, now.step = 0;
    vis[stx][sty][stz] = true; Q.push(now);
    while(!Q.empty()) {
        now = Q.front(); Q.pop();
        if(now.x == edx && now.y == edy && now.z == edz) return now.step;
        for(int i = 1; i <= 6; ++i) {
            next.x = now.x + dx[i];
            next.y = now.y + dy[i];
            next.z = now.z + dz[i];
            next.step = now.step + 1;
            if(!check(next.x, next.y, next.z)) continue;
            vis[next.x][next.y][next.z] = true;
            Q.push(next);
        }
    } 
    return 0;
}

int main() {
    while(scanf("%d%d%d", &k, &n, &m) && (n || m || k)) {
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < k; ++i) for(int j = 0; j < n; ++j) {
            scanf("%s", map[i][j]);
            for(int r = 0; r < m; ++r) {
                if(map[i][j][r] == 'S') stx = i, sty = j, stz = r;
                else if(map[i][j][r] == 'E') edx = i, edy = j, edz = r;
            }
        }
        int ans = bfs();
        if(ans) printf("Escaped in %d minute(s).\n", ans);
        else puts("Trapped!");
    }
    return 0;
}

 

DAY3 搜尋演算法鞏固

POJ 3009 第一眼看上去是個模擬+DFS 結果模擬各種WA自閉了 乾脆直接寫DFS+回溯了

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int W, H, ans;
int stx, sty, edx, edy;
int map[30][30];
int dx[5] = {0, 0, 1, 0, -1}; 
int dy[5] = {0, 1, 0, -1, 0};

inline void dfs(int x, int y, int step) {
	if(step > 10) return;
	for(int i = 1; i <= 4; ++i) {
		int nx = x + dx[i], ny = y + dy[i];
		if(nx < 1 || nx > H || ny < 1 || ny > W || map[nx][ny] == 1) continue;
		while(nx >= 1 && nx <= H && ny >= 1 && ny <= W && map[nx][ny] != 1) {
			if(nx == edx && ny == edy) { ans = min(ans, step + 1); break; }
			nx += dx[i],ny += dy[i];
		}
		if(nx == edx && ny == edy) continue;
		if(nx < 1 || nx > H || ny < 1 || ny > W) continue;
		map[nx][ny] = 0;
 		dfs(nx - dx[i], ny - dy[i], step + 1);
		map[nx][ny] = 1;
	}
}

int main() {
	while(scanf("%d%d", &W, &H) && W && H) {
		memset(map, -1, sizeof(map)); ans = 11;
		for(int i = 1; i <= H; ++i) for(int j = 1; j <= W; ++j) {
			scanf("%d", &map[i][j]);
			if(map[i][j] == 2) stx = i, sty = j;
			if(map[i][j] == 3) edx = i, edy = j;
		}
		dfs(stx, sty, 0);
		if(ans <= 10) printf("%d\n", ans);
		else puts("-1");
	}
	return 0;
}