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:
H, W = map(int, input().split())
S = [input() for i in range(H)]

for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      bk = sum([t[j].count("#") for t in S[max(0,i-1):min(H,i+2)]])
      bk += S[i][max(0,j-1):min(W,j+2)].count("#")-1
      if bk == 1:
        print("No")
        exit()

print("Yes")

Submission status: Accepted

Solution code:
temp=list(map(lambda x:int(x),input().split()))
H,W=temp[0],temp[1]
grid=[]


for _ in range(H):
    row=list(input())
    row.insert(0,'pad')
    row.append('pad')
    grid.append(row)

pad=['pad' for _ in range(W+2)]
grid.insert(0,pad)
grid.append(pad)

# print(np.array(grid))
dx=[1,0,-1,0]
dy=[0,1,0,-1]
flag=True
for r in range(1,H+1):
    for c in range(1,W+1):
        if grid[r][c]=='#':
            cnt=0
            for i in range(4):
                if grid[r+dx[i]][c+dy[i]]=='#':
                    cnt+=1
            if cnt==0:
                flag=False
                break
    if flag==False:break

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

Submission status: Accepted

Solution code:
hw = input().split(" ");
h = int(hw[0]);
w = int(hw[1]);
pic = [['.']*(w+2) for i in range(h+2)];
can = 0;
for i in range(1,h+1) :
	s = list(input().rstrip('\n'));
	for j in range(1,w+1) :
		pic[i][j] = s[j-1];
for i in range(1,h+1) :
	for j in range(1,w+1) :
		if pic[i][j] == '#' and pic[i][j-1] == '.' and pic[i-1][j] == '.' and pic[i][j+1] == '.' and pic[i+1][j] == '.' :
			can = can + 1;
if can == 0 :
	print('Yes');
else :
	print('No');

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
campus=[]
for _ in range(H):
    campus.append(input())
    
def ok(y,x):
    if campus[y][x]==".":
        return(True)
    else:
        TRUE=0
        if y==0:
            pass
        else:
            if campus[y-1][x]=="#":
                TRUE=1
        if y==H-1:
            pass
        else:
            if campus[y+1][x]=="#":
                TRUE=1
        if x==0:
            pass
        else:
            if campus[y][x-1]=="#":
                TRUE=1
        if x==W-1:
            pass
        else:
            if campus[y][x+1]=="#":
                TRUE=1
        if TRUE==1:
            return(True)
        else:
            return(False)

achievement=0            
for i in range(H):
    for j in range(W):
        if ok(i,j):
            pass
        else:
            achievement+=1
            
if achievement==0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H,W = (int(i) for i in input().split())

goal = []
for i in range(H):
    goal.append(input())

canComplete = True

for row in range(H):
    for column in range(W):
        above = row - 1
        bottom = row + 1
        left = column - 1
        right = column + 1
        
        if goal[row][column] == "#":
            if above >= 0 and goal[above][column] == "#":
                continue
            elif bottom < H and goal[bottom][column] == "#":
                continue
            elif left >= 0 and goal[row][left] == "#":
                continue
            elif right < W and goal[row][right] == "#":
                continue
            else:
                canComplete = False
                break

if canComplete:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
# Setup
import sys
sys.setrecursionlimit(4100000)

h, w = [int(i) for i in input().split()]
a = [[str(i) for i in input()] for _ in range(h)]

def check(row,col):
    if row < 0 or col < 0 or row >= h or col >= w:
        return False
    if a[row][col] == ".":
        return False
    return True

def bfs(row, col):
    if any([check(row-1,col), check(row+1, col), check(row,col-1), check(row,col+1)]):
        return True
    return False
end = 0
for i in range(h):
    if end == 0:
        for k in range(w):
            if end == 0:
                if a[i][k] == "#":
                    if bfs(i,k) == False:
                        end = 1
if end == 0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w=[int(i) for i in input().split()]
s=[[i for i in input()] for _ in range(h)]
flg=True
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            flg=False
            for y,x in [(1,0),(-1,0),(0,1),(0,-1)]:
                try:
                    if s[i+y][j+x]=="#":
                        flg=True
                except:
                    pass
        if not flg:
            break
    if not flg:
        break
print("Yes" if flg else "No")

Submission status: Accepted

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

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

Submission status: Accepted

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

S = []

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

flag = True

for i in range(H):
    for j in range(W):
        s_list = []
        if S[i][j] == "#":
            # right
            if j + 1 < W:
                s_list.append(S[i][j + 1])
            # left
            if j - 1 >= 0:
                s_list.append(S[i][j - 1])
            # up
            if i - 1 >= 0:
                s_list.append(S[i - 1][j])
            # down
            if i + 1 < H:
                s_list.append(S[i + 1][j])
            if "#" not in s_list:
                # print(i, j, s_list)
                flag = False


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

Submission status: Accepted

Solution code:
l = input().split()
H = int(l[0])
W = int(l[1])

S = []

for i in range(H):
    S.append(input().split()[0])

# print(H, W, S)

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

Submission status: Accepted

Solution code:
def has_black_around(table, y, x):
    dxs = [0, 1, 0, -1]
    dys = [1, 0, -1, 0]

    for dx, dy in zip(dxs, dys):
        ny = y + dy
        nx = x + dx
        if table[ny][nx] == "#":
            return True
    return False


def solve(table, H, W):

    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if table[y][x] == '#' and not has_black_around(table, y, x):
                return False
    return True


if __name__ == '__main__':
    H, W = map(int, input().split())

    table = [["@" for j in range(W + 2)] for i in range(H + 2)]

    for i in range(1, H + 1):
        table[i][1:W+1] = [ch for ch in input().strip()]

    answer = "Yes" if solve(table, H, W) else "No"

    print(answer)

Submission status: Accepted

Solution code:
from sys import stdin
H,W = [int(x) for x in stdin.readline().rstrip().split()]
data = [["."]*(W+2)]
for _ in range(H):
    data.append(["."] + list(input()) + ["."])
data.append(["."]*(W+2))

ans_flag = True
for h in range(1,H+1):
    for w in range(1,W+1):
        flag = True
        if data[h][w] == "#":
            flag = False
            if data[h+1][w] == "#":
                flag = True
            if data[h-1][w] == "#":
                flag = True
            if data[h][w+1] == "#":
                flag = True
            if data[h][w-1] == "#":
                flag = True
        #print(flag,h,w)
        if flag == False:
            ans_flag = False
if ans_flag:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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


    answer = "Yes"

    for x in range(H):
        for y in range(W):
            if s_list[x][y] == '.':
                continue
            is_all_white = True
            dxs = [0, 1, 0, -1]
            dys = [1, 0, -1, 0]
            for dx, dy in zip(dxs, dys):
                nx = x + dx
                ny = y + dy
                if 0 <= nx <= H-1 and 0 <= ny <= W-1:
                    if s_list[nx][ny] == "#":
                        is_all_white = False
                        break
            if is_all_white:
                answer = "No"
                break
        if s_list[x][y] == '#' and is_all_white:
            break

    print(answer)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
import sys
dxy = [(1,0),(0,1),(-1,0),(0,-1)]
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            flag = 0
            for dy,dx in dxy:
                if 0<= i+dy < H and 0 <= j+dx < W and s[i+dy][j+dx] == "#":
                    flag = 1
            if flag == 0:
                print("No")
                sys.exit(0)
print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
graph = []
for i in range(H+2):
    if i == 0 or i == H+1 :
        graph.append(["." for i in range(W+2)])
    else:
        a = ["."] + list(input()) + ["."]
        graph.append(a)

"""
##となっていれば良い　反対に#が独立して存在する場合にはNo
"""
for i in range(1,H+1):
    for j in range(1,W+1):
        if graph[i][j] == "#":
            if "#" in [graph[i][j+1],graph[i][j-1],graph[i+1][j],graph[i-1][j]] :
                pass
            else : 
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for i in range(H)]
dy = [0,1,0,-1]
dx = [1,0,-1,0]
for y in range(H):
    for x in range(W):
        if S[y][x] == '.':
            continue
        cnt = 0
        for i in range(4):
            ny = y+dy[i]
            nx = x+dx[i]
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == '#':
                cnt += 1
        if cnt == 0:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = [int(hw) for hw in input().split()]
S = [["."]*(W+2)]
for h in range(H):
  S.append(["."]+[s for s in input()]+["."])
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]==".")and(S[h+1][w]==".")and(S[h][w-1]==".")and(S[h][w+1]=="."):
        print("No")
        break
  else:
    continue
  break
else:
  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(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] == ".":
        ans = "No"
        break
        
if s[0][0] == "#":
  if s[0][1] == "." and s[1][0] == ".":
    ans = "No"
if s[0][w-1] == "#":
  if s[0][w-2] == "." and s[1][w-1] == ".":
    ans = "No"
if s[h-1][0] == "#":
  if s[h-2][0] == "." and s[h-1][1] == ".":
    ans = "No"
if s[h-1][w-1] == "#":
  if s[h-2][w-1] == "." and s[h-1][w-1] == ".":
    ans = "No"

for i in range(1,h-1):
  if s[i][0] == "#":
    if s[i-1][0] == "." and s[i+1][0] == "." and s[i][1] == ".":
      ans = "No"
  if s[i][w-1] == "#":
    if s[i-1][w-1] == "." and s[i+1][w-1] == "." and s[i][w-2] == ".":
      ans = "No"
for i in range(1,w-1):
  if s[0][i] == "#":
    if s[0][i-1] == "." and s[0][i+1] == "." and s[1][i] == ".":
      ans = "No"
  if s[h-1][i] == "#":
    if s[h-1][i-1] == "." and s[h-1][i+1] == "." and s[h-2][i] == ".":
      ans = "No"
print(ans)

Submission status: Accepted

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

dxy = [(1,0),(0,1),(0,-1),(-1,0)]
flag = False

for y in range(h):
    for x in range(w):
        if lst[y][x] == ".":
            continue
        sum_count = len(dxy)
        dot_count = 0
        for dx,dy in dxy:
            if x+dx < 0 or x+dx > w-1 or y+dy < 0 or y+dy > h-1:
                sum_count -= 1
                continue
            if lst[y+dy][x+dx] == ".":
                dot_count += 1
        if sum_count == dot_count:
            flag = True
            break
    if flag:
        break

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

Submission status: Accepted

Solution code:
ans = 'Yes'
h,w = map (int,input().split())
pict = []
for i in range (h) :
      dot = input() + '.'
      
      pict.append(dot)
up = '.' * w
pict.append(up)
left = '.'
for i in range(h) :
      line = tuple (pict[i])
      for d in range(w) :
            if line[d] == '#' :
                  if left == '.' and up[d] == '.' and line[d+1] == '.' and pict[i+1][d] == '.' :
                        ans = 'No'
                        break
            left = line[d]
      up = pict[i]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
H,W=map(int,input().split())
M=[list(list("."+input()+".")) for i in range(H)]
M=[[1 if s=='#' else 0 for s in M[h]] for h in range(H)]
L=np.array(M)
M=np.array(M)
N=[0]*(W+2)
M=np.vstack((M, N))
M=np.vstack((N, M))
Ans=np.zeros((H,W))
for h in range(H):
  for w in range(W):
    if M[h+1][w+1]==0:
      continue
    else:
      Ans[h][w]=M[h+1][w+1]+M[h][w+1]+M[h+2][w+1]+M[h+1][w]+M[h+1][w+2]
if np.any(Ans==1):
  print("No")
else:
  print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for i in range(H)]
dy = [0,1,0,-1]
dx = [1,0,-1,0]
for y in range(H):
    for x in range(W):
        if S[y][x] == '.':
            continue
        if any(0<=y+dy[i]<H and 0<=x+dx[i]<W and S[y+dy[i]][x+dx[i]] == '#' for i in range(4)):
            continue
        print('No')
        exit()
print('Yes')

Submission status: Accepted

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

def count(x, y):
    c = 0
    if x-1 >= 0 and s[x-1][y] =="#":
        c += 1
    if x+1 <= H-1 and s[x+1][y] =="#":
        c += 1
    if y-1 >= 0 and s[x][y-1] =="#":
        c += 1
    if y+1 <= W-1 and s[x][y+1] =="#":
        c += 1
    return c

for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            a = count(i, j)
            if a == 0:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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

for i in range(1, h+1):
  for j in range(1, w+1):
    if li[i][j] == "#":
      flag = False
      if li[i][j+1] == "#" or li[i][j-1] == "#" or li[i+1][j] == "#" or li[i-1][j] == "#":
        flag = True
      if not flag:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if canvas[i][j] == ".": continue
        candraw = False
        for delta in [-1,1]:
            if 0 <= i + delta < h:
                if canvas[i+delta][j]=="#":
                    candraw = True
                    break
            if 0 <= j + delta < w:
                if canvas[i][j+delta]=="#":
                    candraw = True
                    break
        if not candraw:
            print("No")
            exit(0)
print("Yes")

Submission status: Accepted

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

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

for i in range(1,H+1):
  for j in range(1,W+1):
    if S[i][j] == '#':
      for k in range(4):
        if S[i+dire[k][0]][j+dire[k][1]] == '#':
          break
      else:
        print('No')
        exit()
else:
  print('Yes')

Submission status: Accepted

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

grid = list()

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

x = [1,-1,0 ,0]   
y = [0, 0,1,-1]   

count = 0

for i in range(h):
    for j in range(w):
        count = 0
        if grid[i][j] == '#':
            for s,t in zip(x,y):
                if h > i+s >= 0 and w > j+t >= 0 and grid[i+s][j+t] == '#':
                    break
            else:
                print("No")
                exit()
                
print("Yes")

Submission status: Accepted

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


def check(h, w):
    if S[h][w] != '#':
        return True
    ps = [(h, w-1), (h+1, w), (h, w+1), (h-1, w)]
    ps = [p for p in ps if p[0] in range(H) and p[1] in range(W)]
    if any(S[p[0]][p[1]] == '#' for p in ps):
        return True
    else:
        return False

if any(not check(h, w) for h in range(H) for w in range(W)):
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[]
f=0
nf=0
for i in range(n):
    l.append(list(input()))
    
for i in range(n):
    for j in range(m):
        if l[i][j]=='#':
            if (i>0 and l[i-1][j]=='#') or (j>0 and l[i][j-1]=='#') or (i<n-1 and l[i+1][j]=='#') or (j<m-1 and l[i][j+1]=='#'):
                f=0
            else:
                nf=1
if nf==1:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
campus = list(input() for i in range(H))


def check_around(x, y):
    if 0 < y and campus[y - 1][x] == '#':
        return True
    elif 0 < x and campus[y][x - 1] == '#':
        return True
    elif x < W - 1 and campus[y][x + 1] == '#':
        return True
    elif y < H - 1 and campus[y + 1][x] == '#':
        return True
    return False


can = True
for y in range(H):
    for x in range(W):
        if campus[y][x] == '#':
            can = check_around(x, y)
            if not can:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]

s = ["." * (w + 2)]
for i in range(h):
    s.append("." + input() + ".")
s.append("." * (w + 2))

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

Submission status: Accepted

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

H, W = mi()
cells = [input() for _ in range(H)]

ispossible = 1
for y, row in enumerate(cells):
  for x, cell in enumerate(row):
    if cell == '.': continue
    else: #cellが'#'だった場合
      ispossible = 0
      #移動する座標を(0, -1)などの形にしてsetにする
      dirset = {(0,1), (0,-1), (1,0), (-1,0)}
      if y == 0: #一番上だった場合
        dirset.remove((0,-1)) #上にはいかない
      elif y == H-1: #一番下だった場合
        dirset.remove((0,1)) #下にはいかない
      if x == 0: #いちばん左だった場合
        dirset.remove((-1,0)) #左にはいかない
      elif x == W-1: #いちばん右だった場合
        dirset.remove((1,0)) #右にはいかない
      for mx, my in dirset: #mx, myはx,yの移動量
        if cells[y+my][x+mx] == '#':
          ispossible = 1
          break
      if ispossible == 0:
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j] =="#":
            if i == 0:
                if j == 0:
                    if a[i+1][j] == "." and a[i][j+1] == ".":
                        print("No")
                        exit()
                elif j == w-1:
                    if a[i+1][j] == "." and a[i][j-1] == ".":
                        print("No")
                        exit()
                else:
                    if a[i+1][j] == "." and a[i][j+1] == "." and a[i][j-1] == ".":
                        print("No")
                        exit()
            elif i == h-1:
                if j == 0:
                    if a[i-1][j] == "." and a[i][j+1] == ".":
                        print("No")
                        exit()
                elif j == w-1:
                    if a[i-1][j] == "." and a[i][j-1] == ".":
                        print("No")
                        exit()
                else:
                    if a[i-1][j] == "." and a[i][j+1] == "." and a[i][j-1] == ".":
                        print("No")
                        exit()
            else:
                if j == 0:
                    if a[i+1][j] == "." and a[i-1][j] == "." and a[i][j+1] == ".":
                        print("No")
                        exit()
                elif j == w-1:
                    if a[i+1][j] == "." and a[i-1][j] == "." and a[i][j-1] == ".":
                        print("No")
                        exit()
                else:
                    if a[i+1][j] == "." and a[i-1][j] == "." and a[i][j+1] == "." and a[i][j-1] == ".":
                        print("No")
                        exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]
s = []
flag = True
for hi in range(h):
    s1 = input()
    s.append(s1)

for hi in range(h):
    for wi in range(w):
        if s[hi][wi] == "#":
            if hi != 0 and s[hi-1][wi] == "#":
                pass
            elif hi != h - 1 and s[hi+1][wi] == "#":
                pass
            elif wi != 0 and s[hi][wi-1] == "#":
                pass
            elif wi != w - 1 and s[hi][wi+1] == "#":
                pass
            else:
                flag = False
    if not flag:
        break

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

Submission status: Accepted

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

for hi in range(h):
    for wi in range(w):
        if m[hi][wi]=="#":
            if not ((hi!=0 and m[hi-1][wi]=="#") or
                    (wi!=0 and m[hi][wi-1]=="#") or
                    (hi!=h-1 and m[hi+1][wi]=="#") or
                    (wi!=w-1 and m[hi][wi+1]=="#")):
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
def is_paintable(h,w,matrix):
    # 上
    if h-1 >= 0 and w >= 0:
        if(matrix[h-1][w] == '#'):
            return True
        
    # 左
    if h >= 0 and w-1 >= 0:
        if(matrix[h][w-1] == '#'):
            return True
    
    # 右
    if h >= 0 and w+1 < W:
        if(matrix[h][w+1] == '#'):
            return True

    # 下
    if h+1 < H and w >= 0:
        if(matrix[h+1][w] == '#'):
            return True
            
    return False

H,W = map(int,input().split())

matrix = []
for i in range(H):
    matrix.append(list(input()))

paintable = True
for h in range(H):
    for w in range(W):
        if matrix[h][w] == '#':
            if(not is_paintable(h, w, matrix)):
                paintable = False
                break
    if(not paintable):
        break

if(paintable):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H, W = [int(x) for x in input().split()]
board = [['.']*(W+2)]
for _ in range(H):
    board.append(['.'] + [x for x in input()] + ['.'])
board.append(['.']*(W+2))
   
cnt = 0
for i in range(1, H+1):
    for j in range(1, W):
        if board[i][j] == '#' and board[i][j+1] != '#' and board[i][j-1] != '#' and board[i-1][j] != '#' and board[i+1][j] != '#':
            cnt += 1

print('No') if cnt else print('Yes')

Submission status: Accepted

Solution code:
def search(X, Y, MAP, H, W):
    if X < 0 or X >= H or Y < 0 or Y >= W:
        return False
    elif MAP[X][Y] == '.':
        return False
    else: 
        return True

def each(X, Y, MAP, H, W):
    right = search(X, Y+1, MAP, H, W)
    down = search(X+1, Y, MAP, H, W)
    left = search(X, Y-1, MAP, H, W)
    up = search(X-1, Y, MAP, H, W)
    return (right or down or left or up)

def main():
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                if not each(i, j, s, h, w):
                    print("No")
                    exit()
    print("Yes")

main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
p = [input() for i in range(h)]
for y in range(h):
    for x in range(w):
        j=0
        if p[y][x]=="#":
            if x>0 and p[y][x-1]=="#":j=1
            elif j==0 and y>0 and p[y-1][x]=="#":j=1
            elif j==0 and x!=w-1 and p[y][x+1]=="#":j=1
            elif j==0 and y!=h-1 and p[y+1][x]=="#":j=1
            if j==0:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
l = [['.']*(w+2)]
for i in range(h):
    row = list(input()) #一文字ずつリストに
    l.append(['.']+row+['.'])
l.append(['.']*(w+2))

'''for i in l:
    print(i)'''

flag = True

for i in range(h+2):
    for j in range(w+2):
        if l[i][j] == '#':
            if l[i-1][j]==l[i][j-1]==l[i+1][j]==l[i][j+1]=='.':
                flag = False

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

Submission status: Accepted

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

L_input = [["." for i in range(W)] for j in range(H)]
L_width = [["x" for i in range(W)] for j in range(H)]
L_height = [["x" for i in range(W)] for j in range(H)]

flag = 0



for i in range(H) : 
    tmp = list(input())
    for j in range(W) : 
        L_input[i][j] = tmp[j]

for i in range(H) : 
    for j in range(W-1) : 
        if L_input[i][j] == "#" and L_input[i][j+1] == "#" : 
            L_width[i][j] = "o"
            L_width[i][j+1] = "o"

for j in range(W) : 
    for i in range(H-1) : 
        if L_input[i][j] == "#" and L_input[i+1][j] == "#" : 
            L_height[i][j] = "o"
            L_height[i+1][j] = "o"
            
for i in range(H) : 
    for j in range(W) : 
        if L_input[i][j] == "#" : 
            if L_width[i][j] != "o" and L_height[i][j] != "o" : 
                flag = 1
                
if flag == 0 : 
    print("Yes")
else : 
    print("No")

Submission status: Accepted

Solution code:
def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    field = []
    for row in inputs:
        line = []
        for c in row:
            line.append(c)
        field.append(line)

    if len(field) == 1:
        if len(field[0]) == 1:
            return "No"

    for i, row in enumerate(field):
        for j, _ in enumerate(row):
            available = False
            if field[i][j] == ".":
                continue
            if i - 1 >= 0:
                available |= field[i - 1][j] == "#"

            if j - 1 >= 0:
                available |= field[i][j - 1] == "#"
            if j + 1 < len(row):
                available |= field[i][j + 1] == "#"

            if i + 1 < len(field):
                available |= field[i + 1][j] == "#"
            if not available:
                return "No"

    return "Yes"


if __name__ == "__main__":
    [N, _] = map(lambda x: int(x), input().split())
    ret = solve(inputs(N))
    print(ret)

Submission status: Accepted

Solution code:

def main(s_list):
    if len(s_list) == 1 and s_list[0] == '#':
        print('Yes')
        return

    for i_row, s in enumerate(s_list):
        for j_col, char in enumerate(s):
            if char == '#':
                neighbor_list = []
                try:
                    neighbor_list.append(s_list[i_row-1][j_col])
                except:
                    pass
                try:
                    neighbor_list.append(s_list[i_row+1][j_col])
                except:
                    pass
                try:
                    neighbor_list.append(s_list[i_row][j_col-1])
                except:
                    pass
                try:
                    neighbor_list.append(s_list[i_row][j_col+1])
                except:
                    pass
                
                if neighbor_list.count('#') == 0:
                    print('No')
                    return

    print('Yes')

if __name__ == "__main__":
    H, W = [int(x) for x in input().split()]
    s_list = []
    for i in range(H):
        s_list.append(input())

    main(s_list)

def test1(capsys):
    inputs = ['.#.', '###', '.#.']
    out = 'Yes'
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == out + '\n'

def test2(capsys):
    inputs = ['#.#.#', '.#.#.', '#.#.#', '.#.#.', '#.#.#']
    out = 'No'
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == out + '\n'

def test3(capsys):
    inputs = ['...#####...', '.##.....##.', '#..##.##..#', '#..##.##..#', '#.........#', '#...###...#', '.#########.', '.#.#.#.#.#.', '##.#.#.#.##', '..##.#.##..', '.##..#..##.']
    out = 'Yes'
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == out + '\n'

def test4(capsys):
    inputs = ['#']
    out = 'Yes'
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == out + '\n'