题解AGC004C

题目
样例 AGC 好评。
题意:让你在一个 \(H \times W\) 的方格纸上找两个连通块,使得他们的重合部分就是输入中给的部分。
先放个样例。
输入:

5 5
.....
.#.#.
.....
.#.#.
.....

输出:

.....
#####
#....
#####
.....

.###.
.#.#.
.#.#.
.#.#.
.....

你可以看到,把输出的那两个结合一下是这样的:

01110
12121
11011
12121
00000

然后说怎么做。
可以考虑让两个图并起来以后填满整个图,所以我让其中一个图占满奇行,另一个图占满偶行。
怎样是两个图全部联通呢?
再看一眼题目,可以发现一条重要性质:
边界没有 #
所以可以让一张图占满左列,另一张图占满右列。
最后把题目要求的那些重合的点同时加入两个图中即可。不难发现,加入后的图一定是联通的。
根据这个做法,与样例输入配对的输出是:

####.
##.#.
####.
##.#.
####.

....#
#####
....#
#####
....#

第一行和最后一行无所谓。
代码:

#include<stdio.h>
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define ll long long
#define il inline
#define swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define max(i, j) (i) > (j) ? (i) : (j)
#define min(i, j) (i) < (j) ? (i) : (j)
#define read(i) io.READ(i)
#define print(i) io.WRITE(i)
#define push(i) io.PUSH(i)
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  inline char gc() {
#if DEBUG
    return getchar();
#endif
    if(p1 == p2)
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void READ(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc())
      if(ch == '-') sign = 1;
    for(; isdigit(ch); ch = gc())
      x = x * 10 + (ch - '0');
    if(ch == '.')
      for(ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if(sign) x = -x;
  }
  inline void READ(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
    for(; !blank(ch); ch = gc())
      *s++ = ch;
    *s = 0;
  }
  inline void READ(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void PUSH(const char &c) {
#if DEBUG
    putchar(c);
#else
    if(pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
      pp = pbuf;
    }
    *pp++ = c;
#endif
  }
  template <class T>
  inline void WRITE(T x) {
    if(x < 0) {
      x = -x;
      PUSH('-');
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while(x);
    while(top)
      PUSH(sta[--top] + '0');
  }
  template <class T>
  inline void WRITE(T x, char lastChar) {
    WRITE(x);
    PUSH(lastChar);
  }
} io;
int h, w, a[510][510], b[510][510];
char map[510][510];
int main() {
  read(h), read(w);
  rep(i, 1, h) {
    rep(j, 1, w) map[i][j] = getchar();
    getchar();
  }
  int crayon;
  rep(i, 2, h - 1) {
    if(i % 2) crayon = 1;
    else crayon = 0;
    rep(j, 2, w - 1) {
      a[i][j] = crayon;
      b[i][j] = crayon ^ 1;
      if(map[i][j] == '#') a[i][j] = b[i][j] = 1;
    }
  }
  rep(i, 1, h) {
    rep(j, 1, w) putchar((a[i][j] || j == 1) ? '#' : '.');
    puts("");
  }
  puts("");
  rep(i, 1, h) {
    rep(j, 1, w) putchar((b[i][j] || j == w) ? '#' : '.');
    puts("");
  }
  puts("");
  return 0;
}
posted @ 2021-03-27 08:14  1358id  阅读(42)  评论(0编辑  收藏  举报