def change(sr,sc,maze,flag):#定義變化函式(無論是加入木樁還是移除木樁)(sr,sc:木樁位置,maze:場地,flag:判斷加入還是移除=題目的t)
for cr,cc in [(0,-1),(0,1),(-1,0),(1,0)]:#判斷上下左右有沒有木樁"@"
nr,nc=sr+cr,sc+cc
while 0<=nr<row and 0<=nc<col and not(maze[nr][nc]=="@"):#如果超出邊界,停止while-loop避免index out of range
nr,nc=nr+cr,nc+cc
if 0<=nr<row and 0<=nc<col:#沒有超出邊界,代表存在"@"
if cr==0:#r的改變值=0,代表改變的是c
for c in range(min(nc,sc)+1,max(nc,sc)):
if flag:#刪除線時要注意:如果拔掉木樁,但如果仍有其他木樁連線,不能刪掉
if not("@" in [maze[a][c] for a in range(sr)] and "@" in [maze[a][c] for a in range(sr+1,row)]):#檢測針對每個col值,將上下的值加到陣列,檢測是否兩邊都有"@",如果符合,就不刪除
maze[sr][c]="_" #刪除線轉成無
else:
maze[sr][c]="*"#加入線
else:#r值改變
for r in range(min(nr,sr)+1,max(nr,sr)):
if flag:#原理類似
if not("@" in maze[r][:sc] and "@" in maze[r][sc+1::]):
maze[r][sc]="_"
else:
maze[r][sc]="*"
if flag:maze[sr][sc]="_"#移除"@"
else:maze[sr][sc]="@"#加入"@"
return maze,row*col-sum([item.count("_") for item in maze])
row,col,n=map(int,input().split())
ans=0#紀錄最大值
maze=[["_"]*col for _ in range(row)]#初始化場地
for _ in range(n):
r,c,t=list(map(int,input().split()))
maze,now=change(r,c,maze,t)
ans=max(ans,now)#ans和now比較,如果now比較大就變成now
print(ans)
print(now)
def change(sr,sc,maze,flag):#定義變化函式(無論是加入木樁還是移除木樁)(sr,sc:木樁位置,maze:場地,flag:判斷加入還是移除=題目的t)
for cr,cc in [(0,-1),(0,1),(-1,0),(1,0)]:#判斷上下左右有沒有0
nr,nc=sr+cr,sc+cc
while 0<=nr<row and 0<=nc<col and not(maze[nr][nc]==1):#如果超出邊界,停止while-loop避免index out of range
nr,nc=nr+cr,nc+cc
if 0<=nr<row and 0<=nc<col:#沒有超出邊界,代表存在有0
if cr==0:#r的改變值=0,代表改變的是c(橫線)
for c in range(min(nc,sc)+1,max(nc,sc)):
if flag:
maze[sr][c]=max(0,maze[sr][c]-4) #防止刪除沒有線的地方,產生負值
else:
if not(maze[sr][c] in (4,9)):#防止增加已經有橫線的地方,產生8或其他
maze[sr][c]+=(4)
else:#r值改變(直線)
for r in range(min(nr,sr)+1,max(nr,sr)):
if flag:
maze[r][sc]=max(0,maze[r][sc]-5) #防止刪除沒有線的地方,產生負值,
else:
if not(maze[r][sc] in (5,9)):#防止增加已經有直線的地方,產生10或其他
maze[r][sc]+=(5)
if flag:maze[sr][sc]=0#移除0
else:maze[sr][sc]=1#添加1
return maze,row*col-sum([item.count(0) for item in maze])
row,col,n=map(int,input().split())
ans=0#紀錄最大值
maze=[[0]*col for _ in range(row)]#初始化場地
for _ in range(n):
r,c,t=list(map(int,input().split()))
maze,now=change(r,c,maze,t)
ans=max(ans,now)#ans和now比較,如果now比較大就變成now
print(ans)
print(now)
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing