Problem:


Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.  

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
  :   :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

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

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes



Sample input:
3 3
.#.
###
.#.


Sample output:
Yes

Submission status: Accepted

Solution code:
def check(m, i, j, H, W):
    left = False
    up = False
    right = False
    down = False
    left = ((j == 0) or (m[i][j-1] == "#"))
    up = ((i == 0) or (m[i-1][j] == "#"))
    right = ((j == (W - 1)) or (m[i][j+1] == "#"))
    down = ((i == (H-1)) or (m[i+1][j] == "#"))
    return left or right or up or down
  
H, W = map(int, input().split())
l = [str(input()) for i in range(H)]
ans = "Yes"
for i in range(H):
    for j in range(W):
        if (l[i][j] == "#") and (not(check(l, i, j, H, W))):
            ans = "No"
print(ans)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())

grid = []

for i in range(h):
    grid.append(list(input()))

dir = [[0,1],[-1,0],[0,-1],[1,0]]

for i in range(h):
    for j in range(w):
        flg = 0
        if grid[i][j] == '#':
            for k in range(4):
                if 0 <= i + dir[k][0] <= h-1 and 0 <= j + dir[k][1] <= w-1:
                    if grid[i + dir[k][0]][j + dir[k][1]] == '#':
                        flg = 0
                        break
                    flg = 1
        if flg == 1:
            break
    if flg == 1:
        break

if flg == 1:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
L = []
for i in range(H):
    L.append(input())
    
ans = "Yes"
for i in range(H):
    for j in range(W):
        if L[i][j] == "#":
            temp = []
            if i != 0:
                temp.append(L[i-1][j])
            if i != H-1:
                temp.append(L[i+1][j])
            if j != 0:
                temp.append(L[i][j-1])
            if j != W-1:
                temp.append(L[i][j+1])
            if "#" not in temp:
                ans = "No"
                break

print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

d = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            continue

        flag = False
        for dx, dy in d:
            i2, j2 = i+dx, j+dy
            if not (0 <= i2 < H and 0 <= j2 < W):
                continue

            if S[i2][j2] == "#":
                flag = True

        if not flag:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input() for _ in range(H)]
ans='Yes'
for i in range (H):
  for j in range (W):
    if S[i][j]=='#':
      if S[max(0,i-1)][j]!='#' and S[min(i+1,H-1)][j]!='#' and S[i][max(0,j-1)]!='#' and S[i][min(j+1,W-1)]!='#':
        ans='No'
        break
print(ans)

Submission status: Accepted

Solution code:
h,w=map(int, input().split())
# 余白付け足し
s=['.'*(w+2)]
for _ in range(h):
    s.append('.'+input()+'.')
s.append('.'*(w+2))
    
# 全ての#マスにおいて、縦横のいずれかが#であればOK
for i in range(h+1):
    for j in range(w+1):
        if s[i][j]=="#":
            if s[i-1][j] == s[i+1][j] == s[i][j-1] == s[i][j+1] == ".":
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
g = [['.'] * (w+2) for i in range(h+2)]
for i in range(h):
  g[i+1][1:-1] = input()
for i in range(h):
  for j in range(w):
    if g[i+1][j+1] == '#':
      f = 1
      for a,b in [[1,0],[0,1],[-1,0],[0,-1]]:
        if g[i+a+1][j+b+1] == '#':
          f = 0
          break
      if f:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
board = [ [0 for _ in range(w)] for _ in range(h) ]
for i in range(h):
  s = input()
  for j, c in enumerate(s):
    if c == '#':
      board[i][j] = 1

for i in range(h):
  c0 = board[i][0]
  for j in range(1, w):
    c = board[i][j]
    if c == 0:
      continue
    if board[i][j-1] == 1 or (j < w-1 and board[i][j+1] == 1):
      continue
    if (i > 0 and board[i-1][j] == 1) or (i < h-1 and board[i+1][j] == 1):
      continue
    print('No')
    exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            continue
        if i != 0:
            if s[i - 1][j] == '#':
                continue
        if i != H - 1:
            if s[i + 1][j] == '#':
                continue
        if j != 0:
            if s[i][j - 1] == '#':
                continue
        if j != W - 1:
            if s[i][j + 1] == "#":
                continue

        print('No')
        sys.exit()

print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
A=[]

for i in range(H):
    S='.'+input()+'.'
    A.append(S)
K='.'
A.insert(0,K*(W+2))
A.append(K*(W+2))
for i in range(1,H):
    for j in range(1,W):
        if A[i][j]=='#':
            if A[i+1][j]=='.' and A[i-1][j]=='.' and A[i][j+1]=='.' and A[i][j-1]=='.':
                print('No')
                exit(0)

print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int, input().split())
t=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if t[i][j]==".":continue
        if t[i-1][j]=="." and t[i+1][j]=="." and t[i][j-1]=="." and t[i][j+1]==".":
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
 
flag = False
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            if (j-1 >= 0 and s[i][j-1] == '.') and (j+1 < W and s[i][j+1] == '.') and (i-1 >= 0 and s[i-1][j] == '.') and (i+1 < H and s[i+1][j] == '.'):
                print('No')
                flag = True
                break
    if flag:
        break
else:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
Ss = [[j for j in input()] for i in range(H)]

mapping = {'.':0, '#':1}

ans = 'Yes'
for i in range(H):
    for j in range(W):
        if ans == 'No':
          continue
        if Ss[i][j] == '#':
            counter = 0
            
            if j>0:
                counter += mapping[Ss[i][j-1]]
            if i>0:
                counter += mapping[Ss[i-1][j]]
            if i<H-1:
                counter += mapping[Ss[i+1][j]]
            if j<W-1:
                counter += mapping[Ss[i][j+1]]
      
            if counter == 0:
                ans = 'No'
            
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def main():
    H, W = map(int, input().split())
    S = [list(input()[:-1]) for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j] != '#':
                continue

            flg = True
            if i-1>=0 and S[i-1][j]=='#':
                flg = False
            if i+1<H and S[i+1][j]=='#':
                flg = False
            if j-1>=0 and S[i][j-1]=='#':
                flg = False
            if j+1<W and S[i][j+1]=='#':
                flg = False
            
            if flg:
                print('No')
                return
    
    print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
L=[list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if L[i][j]=='#':
            if L[i][max(j-1,0)]!='#' and L[i][min(j+1,w-1)]!='#' and L[max(i-1,0)][j]!='#' and L[min(i+1,h-1)][j]!='#':
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

flag = False
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            if (j-1 >= 0 and s[i][j-1] == '.') and (j+1 < W and s[i][j+1] == '.') and (i-1 >= 0 and s[i-1][j] == '.') and (i+1 < H and s[i+1][j] == '.'):
                print('No')
                flag = True
                break
    if flag:
        break
else:
    print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
#print(S)

def lonely(i,j):
  if S[i][j]=="#":
    res=0
    if i-1>=0: res+=1 if S[i-1][j]=="#" else 0
    if i+1<H: res+=1 if S[i+1][j]=="#" else 0
    if j-1>=0: res+=1 if S[i][j-1]=="#" else 0
    if j+1<W: res+=1 if S[i][j+1]=="#" else 0
    #print(i,j,res)
    return res
  else:
    return 1

ans="Yes"
for i in range(H):
  for j in range(W):
    if lonely(i,j)==0:
      ans="No"
print(ans)

Submission status: Accepted

Solution code:
def main():
    h,w=map(int, input().split())
    t=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if t[i][j]==".":continue
            if t[i-1][j]=="." and t[i+1][j]=="." and t[i][j-1]=="." and t[i][j+1]==".":
                print("No")
                exit()
    print("Yes")

main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = ['.' * (W + 2)]
for i in range(H):
    grid.append('.' + input() + '.')
grid += ['.' * (W + 2)]
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if grid[i][j] == "#":
            if not (grid[i][j + 1] == "#" or grid[i + 1][j] == "#"
                    or grid[i][j - 1] == "#" or grid[i - 1][j] == "#"):
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys

def solve():
    H, W = map(int, sys.stdin.readline().split())
    S = ['.' + sys.stdin.readline().rstrip() + '.' for _ in range(H)]
    S.insert(0,'.'*(W+2))
    S.append('.'*(W+2))
    for h in range(1,H+1):
        for w in range(1,W+1):
            if S[h][w] == '#':
                if '.' == S[h-1][w] == S[h][w-1] == S[h][w+1] == S[h+1][w]:
                    print('No')
                    exit()
    print('Yes')
    
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        H, W = map(int, input().split())
        s = [input().rstrip() for _ in range(H)]

        for i in range(H):
            s[i] = '.' + s[i] + '.'
        frame = ['.' * (W + 2)]
        s = frame + s + frame

        for h in range(1, H + 1):
            for w in range(1, W + 1):
                if s[h][w] == '#':
                    cnt = 0
                    for lr, ud in (0, -1), (0, 1), (-1, 0), (1, 0):
                        if s[h + lr][w + ud] == '#':
                            cnt += 1
                    if cnt == 0:
                        print('No')
                        exit()
        print('Yes')


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
pic = []
pic.append(["."] * (w + 2))
for i in range(h):
  pic.append(["."] + list(input()) + ["."])
pic.append(["."] * (w + 2))


can_paint = True
for i in range(1, h+1):
  for j in range(1, w+1):
    if pic[i][j] == "#":
      up = pic[i-1][j]
      down = pic[i+1][j]
      left = pic[i][j-1]
      right = pic[i][j+1]
      if up == "." and down == "." and left == "." and right == ".":
        can_paint = False
      
if can_paint == True:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())

total = []
tmp = ''
for i in range(w+2):
    tmp += '.'
total.append(tmp)

for i in range(h):
    tmp = '.'
    row = input()
    tmp += row
    tmp += '.'
    total.append(tmp)

tmp = ''
for i in range(w+2):
    tmp += '.'
total.append(tmp)

flag = 0
for i in range(1, h+1):
    for j in range(1, w+1):
        if total[i][j] == '#' and total[i-1][j] == '.' and total[i+1][j] == '.' and total[i][j-1] == '.' and total[i][j+1] == '.':
            flag = 1

if flag:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:


import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

H, W = map(int, input().split())
S = []
for i in range(H):
    s = list(input().rstrip())
    S.append(s)

#print(S)

for h in range(H):
    for w in range(W):
        if S[h][w] != '#':
            continue


        # 上
        if h > 0:
            if S[h-1][w] == '#':
                continue

        # 左
        if w > 0:
            if S[h][w-1] == '#':
                continue

        # 下
        if h < H-1:
            if S[h+1][w] == '#':
                continue

        # 右
        if w < W-1:
            if S[h][w+1] == '#':
                continue

        # 全部ダメ
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
a = [input() for _ in range(h)]
y = [1,-1,0, 0]
x = [0, 0,1,-1]
flag = True
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            for k in range(4):
                if i + y[k] < 0 or i + y[k] >= h or j + x[k] < 0 or j + x[k] >= w:continue
                if a[i+y[k]][j+x[k]] == "#":
                    break
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

hw = ['.' * (W+2)]
for i in range(H):
  hw.append('.' + input() + '.')
hw.append('.' * (W+2)) 
  
ans = "Yes"

for i in range(1, H+1):
  for j in range(1, W+1):
    if hw[i][j] == "#":
      if hw[i-1][j] == "." and hw[i][j-1] == "." and hw[i][j+1] == "." and hw[i+1][j] == ".":
        ans = "No"
        
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = [[i for i in input()] for i in range(H)]

jud = 'Yes'
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            try:
                if grid[i-1][j] == grid[i][j-1] == grid[i][j+1] == grid[i+1][j] == '.':
                    jud = 'No'
                else:
                    continue
            except Exception:
                break

print(jud)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

S = [list(input()) for _ in range(H)]

# #の座標(i,j)を与え、隣接している#があるならばTrue、そうでなければNoを出力して終了する
def exist_neighbor_black_cell(i,j):
    if "#" in [S[max(0,i-1)][j], S[min(H-1,i+1)][j], S[i][max(0,j-1)], S[i][min(W-1,j+1)]]:
        return True
    else:
        print("No")
        exit()

# どの#にも隣接していない#マスがあれば不可。そうでなければ可
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            exist_neighbor_black_cell(i, j)

print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

S = [list(input()) for _ in range(H)]  # .を0に置換しておく。文字列にしか置換できないので注意

# #の座標(i,j)を与え、隣接している#があるならばTrue、そうでなければNoを出力して終了する
def exist_neighbor_black_cell(i,j):
    if "#" in [S[max(0,i-1)][j], S[min(H-1,i+1)][j], S[i][max(0,j-1)], S[i][min(W-1,j+1)]]:
        return True
    else:
        print("No")
        exit()

# どの#にも隣接していない#マスがあれば不可。そうでなければ可
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            exist_neighbor_black_cell(i, j)

print("Yes")

Submission status: Accepted

Solution code:
import itertools
H,W = map(int, input().strip().split(' '))

board = ['.'*(W+2)]
board += ['.'+input().strip()+'.' for _ in range(H)]
board.append('.'*(W+2))

# 全ての黒マスの周囲4マスに黒マスがあればよい
for x,y in itertools.product(range(1,W+1), range(1,H+1)):
  if board[y][x] == '.' : continue
  flag = False
  for dx,dy in [(1,0),(-1,0),(0,1),(0,-1)]:
    if board[y+dy][x+dx] == '#': flag = True
  if not flag:
    print('No')
    exit()
print('Yes')

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    s = [list(str(input().rstrip('\n'))) for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                b = False
                t = [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]
                for ti, tj in t:
                    if 0 <= ti < h and 0 <= tj < w:
                        if s[ti][tj] == "#":
                            b = True
                            break
                if not b:
                    print("No")
                    exit()
    print("Yes")


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
(h, w) = map(int, input().split())
s = [input() for _ in range(h)]


def is_valid(s, i, j):
    if 0 > i or i >= len(s):
        return False
    if 0 > j or j >= len(s[0]):
        return False
    return True


def check(s, i, j):
    if not is_valid(s, i, j):
        return True
    if '.' == s[i][j]:
        return True

    if is_valid(s, i - 1, j + 0):
        if '#' == s[i - 1][j + 0]:
            return True
    if is_valid(s, i + 0, j - 1):
        if '#' == s[i + 0][j - 1]:
            return True
    if is_valid(s, i + 1, j + 0):
        if '#' == s[i + 1][j + 0]:
            return True
    if is_valid(s, i + 0, j + 1):
        if '#' == s[i + 0][j + 1]:
            return True
    return False


for i in range(h):
    for j in range(w):
        if not check(s, i, j):
            print("No")
            exit(0)
print("Yes")

Submission status: Accepted

Solution code:
import sys

h, w = map(int, input().split())
s = [['.'] + list(input()) + ['.'] for i in range(h)]
s = [['.'] * (w+2)] + s + [['.'] * (w+2)]

for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '#':
            if s[i-1][j] == '.' \
                and s[i+1][j] == '.' \
                and s[i][j+1] == '.' \
                and s[i][j-1] == '.':
                print('No')
                sys.exit(0)
print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int, input().split())
S=[[] for _ in range(H)]
for i in range(H):
    S[i]=list(input())

for i in range(H):
    for j in range(W):
        if S[i][j]=='#':
            isOK=False
            if i-1 >= 0 and S[i-1][j]=='#':
                isOK=True
            if j-1 >= 0 and S[i][j-1]=='#':
                isOK=True
            if i+1 <= H-1 and S[i+1][j]=='#':
                isOK=True
            if j+1 <= W-1 and S[i][j+1]=='#':
                isOK=True
            
            if isOK==False:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
SW=[input() for _ in range(h)]

for i in range(1,h-1):
  for j in range(1,w-1):
    if SW[i][j]=='#':
      if SW[i-1][j]=='#' or SW[i+1][j]=='#' or SW[i][j-1]=='#' or SW[i][j+1]=='#':
        pass
      else:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [["."]+list(input())+["."] for i in range(h)]

for i in range(h):
  for j in range(1,w+1):
    if i == 0:
      if s[i][j]=="#" and s[i][j+1]=="." and s[i][j-1]=="." and s[i+1][j]==".":
        print("No")
        exit()
    elif i == h-1:
      if s[i][j]=="#" and s[i][j+1]=="." and s[i][j-1]=="." and s[i-1][j]==".":
        print("No")
        exit()
    else:
      if s[i][j]=="#" and s[i][j+1]=="." and s[i][j-1]=="." and s[i+1][j]=="." and s[i-1][j]==".":
        print("No")
        exit()
        
print("Yes")

Submission status: Accepted

Solution code:
# ABC096C - Grid Repainting 2
def main():
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for x, s in enumerate(S):
        for y, a in enumerate(s):
            if a == "#":
                for dx, dy in dxy:
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                        break
                else:
                    print("No")
                    return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

h,w=map(int,input().split())
s=[list(input().rstrip()) for _ in range(h)]
mas=[["."]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if j+1<w and (s[i][j],s[i][j+1])==("#","#"):
            mas[i][j],mas[i][j+1]="#","#"
        if j-1>=0 and (s[i][j],s[i][j-1])==("#","#"):
            mas[i][j],mas[i][j-1]="#","#"
        if i+1<h and (s[i][j],s[i+1][j])==("#","#"):
            mas[i][j],mas[i+1][j]="#","#"
        if i-1>=0 and (s[i][j],s[i-1][j])==("#","#"):
            mas[i-1][j],mas[i][j]="#","#"
print("Yes" if mas==s else "No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s_list = []
for i in range(H):
    s_list.append(list(input()))

for i in range(H):
    for j in range(W):
        if s_list[i][j] == '.':
            continue
        else:
            if i != 0:
                if s_list[i - 1][j] == '#':
                    continue
            if i != H - 1:
                if s_list[i + 1][j] == '#':
                    continue
            if j != 0:
                if s_list[i][j - 1] == '#':
                    continue
            if j != W - 1:
                if s_list[i][j + 1] == '#':
                    continue
        print("No")
        exit()

print("Yes")

Submission status: Accepted

Solution code:
import sys
a, b = map(int, input().split())
paint = []
paint.append("".join(["."]*(b+2)))
for _ in range(a):
    t = input()
    s = "".join([".", t, "."])
    paint.append(s)
paint.append("".join(["."]*(b+2)))
for i in range(a):
    for j in range(b):
       if paint[i][j] == "#":
           if paint[i+1][j] == "." and paint[i-1][j] == "." and paint[i][j+1] == "." and paint[i][j-1] == ".":
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
zero = ['0' for i in range(w+2)]
S = [['0']+list(input())+['0'] for i in range(h)]
S = [zero] + S + [zero]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j] == '#':
      if S[i+1][j] == '#' \
      or S[i-1][j] == '#' \
      or S[i][j+1] == '#' \
      or S[i][j-1] == '#':
        continue
      else:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
x,y = map(int,input().split())
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(input()))

    except:
        break;
s=0
for i in range(x):
  for j in range(y):
    if  ((listA[i][j]=='#')&(listA[i][j]!=listA[max(0,i-1)][j])&(listA[i][j]!=listA[min(x-1,i+1)][j])&
    (listA[i][j]!=listA[i][max(0,j-1)])&(listA[i][j]!=listA[i][min(y-1,j+1)])):
      s+=1
if s==0:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = ['.' + input() + '.' for _ in range(H)]
S = ['.' * (W+2)] + S + ['.' * (W+2)]

flag = 'Yes'
for i in range(1, H+1):
	for j in range(1, W+1):
		if S[i][j] == '#':
			if S[i-1][j] == '.' and S[i+1][j] == '.' and S[i][j-1] == '.' and S[i][j+1] == '.':
				flag = 'No'
print(flag)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
d = [-1, 0, 1, 0]
for i in range(H):
  for j in range(W):
    if S[i][j] == ".":
      continue
    f = 1
    for k in range(4):
      if 0 <= i + d[k] < H and 0 <= j + d[-1 - k] < W:
        if S[i + d[k]][j + d[-1 - k]] == "#":
          f = 0
          break
    if f:
      print("No")
      exit(0)
print("Yes")

Submission status: Accepted

Solution code:
D = ((-1,0), (0,-1), (1,0), (0,1))
F, f = False, False
H, W = map(int, input().split())
S = [""] * H

for i in range(H):
  S[i] = input()
  
for i in range(H):
  for j in range(W):
    if S[i][j] != ".":
      f = True
      for d in D:
        if (0 <= d[0]+i < H) and (0 <= d[1]+j < W) :
          if S[d[0]+i][d[1]+j] == "#":
            f = False
            break
    if f:
      F = True
if F:
  print("No")
else:
  print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = ["."+input()+"."for i in range(H)]
S = ["."*(W+2)]+S+["."*(W+2)]
flag=0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            if S[i-1][j]== "." and S[i+1][j]=="." and S[i][j-1]=="." and S[i][j+1]==".":
                flag=1
print("Yes" if flag==0 else "No")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [["."]+list(input())+["."] for _ in range(h)]
s.insert(0, ["." for _ in range(w+2)])
s.append(["." for _ in range(w+2)])
ans = "Yes"
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            if not(s[i][j+1]=="#" or s[i][j-1]=="#" or s[i+1][j]=="#" or s[i-1][j]=="#" ):
                ans = "No"
print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [["."]+list(input())+["."] for _ in range(h)]
s.insert(0, ["." for _ in range(w+2)])
s.append(["." for _ in range(w+2)])
ans = "Yes"
# for i in s:
#     print(i)
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            if not(s[i][j+1]=="#" or s[i][j-1]=="#" or s[i+1][j]=="#" or s[i-1][j]=="#" ):
                ans = "No"
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [[""]*W]*H
for i in range(H):
    s[i] = input()
for i in range(1,H-1):
    for j in range(1,W-1):
        if s[i-1][j] == "." and s[i+1][j] == "." and s[i][j+1] == "." and s[i][j-1] == "." and s[i][j] == "#":
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    H, W = map(int, input().split())
    s = []
    for i in range(H):
        s.append(input())
    
    """
    # black
    . white
    """
    
    black = "#"
    white = "."
    
    ans = "Yes"
    break_flag = False
    for i in range(H):
        for j in range(W):
            if s[i][j] == black:
                above = False
                under = False
                left = False
                right = False
                
                if 0 < i:
                    above = (s[i - 1][j] == black)
                if i < H - 1:
                    under = (s[i + 1][j] == black)
                if 0 < j:
                    left = (s[i][j - 1] == black)
                if j < W - 1:
                    right = (s[i][j + 1] == black)
                
                if not (above or under or left or right):
                    break_flag = True
                    ans = "No"
                    #print(i, j)
                    #print(above, under, left, right)
            
            if break_flag:
                break
            
        if break_flag:
            break
    
    print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = ['.' * (w+2)] + ['.' + input() + '.' for _ in [0]*h] + ['.' * (w+2)]
a = 'Yes'
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '#':
            if s[i-1][j] == '.' and s[i+1][j] == '.' and s[i][j-1] == '.' and\
               s[i][j+1] == '.':
                a = 'No'
print(a)

Submission status: Accepted

Solution code:
# ABC096 C - Grid Repainting 2
h, w = map(int,(input().split()))
s= [[0]*(w+2)]*(h+2)
ans= 'Yes'
for i in range(1,h+1):
        s[i] = [0] + list(input()) + [0]

for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '#':
            if s[i+1][j]=='#' or s[i-1][j]=='#' or s[i][j+1]=='#' or s[i][j-1]=='#':
                pass
            else:
                ans='No'
                break

print(ans)

Submission status: Accepted

Solution code:
H, W=map (int, input().split(' '))
z=[]

for i in range(H):
    z.append(list(str(input())))

#上下左右なので斜めは消す
dx = [ 0,  -1,  1,   0, ]
dy = [ 1,   0,  0,  -1, ]

for h in range(H):
    for w in range(W):
        if z[h][w] == '#': 
            p=0
            for i in range(4):
                x = w + dx[i]
                y = h + dy[i]

                if x < 0 or y <0 or x > W-1 or y >H-1:
                    continue

                if z[y][x]=="#":
                    p+=1
            if p==0:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [['.'] + list(input()) + ['.'] for _ in range(h)]
s.insert(0, ['.'] * (w + 2))
s.append(['.'] * (w + 2))
direction = [(-1, 0), (0, -1), (1, 0), (0, 1)]

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '#':
            if all(s[i + dx][j + dy] == '.' for dx, dy in direction):
                print('No')
                exit()

print('Yes')