116B - Little Pigs and Wolves - CodeForces Solution


greedy implementation *1100

Please click on ads to support us..

Python Code:

m, n = list(map(int, input().split()))
arr = list()
ans=0
for i in range(m):
    arr.append(list(input()))
visited = set()
for i in range(m):
    for j in range(n):
        if arr[i][j]=='W':
            for x,y in [(i+1,j), (i-1,j), (i,j+1), (i, j-1)]:
                if x>-1 and y>-1 and x<m and y<n:
                    if arr[x][y] == 'P' and (x,y) not in visited:
                        visited.add((x,y))
                        ans +=1
                        break
print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;







int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);

	freopen("output.txt","w",stdout);
#endif

int n,m;
cin>>n>>m;

vector<vector<char>>d(n,vector<char>(m));



for(int i=0;i<n;i++){
	for(int j=0;j<m;j++){
		cin>>d[i][j];

	}
}


int cnt=0;

for(int i=0;i<n;i++){
	for(int j=0;j<m;j++){

		if(d[i][j]!='W')continue;
		if(i-1>=0){
			if(d[i-1][j]=='P'){
				cnt++;
				d[i-1][j]='.';
				continue;

			}
			
		}
		if(j-1>=0){
			if(d[i][j-1]=='P'){
				cnt++;
				d[i][j-1]='.';
				continue;

			}
			
		}
		if(j+1<m){
			if(d[i][j+1]=='P'){
				cnt++;
				d[i][j+1]='.';
				continue;

			}
			
		}
		if(i+1<n){
			if(d[i+1][j]=='P'){
				cnt++;
				d[i+1][j]='.';
				continue;

			}
			
		}

	}
}

cout<<cnt;


}


Comments

Submit
0 Comments
More Questions

1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2