948A - Protect Sheep - CodeForces Solution


brute force dfs and similar graphs implementation *900

Please click on ads to support us..

Python Code:

def solution():
    rows,columnns=map(int, input().split())
    area=[]
    for i in range(rows):
        row= list(input())
        area.append(row)

    for i in range(rows):
        for j in range(columnns):
            if area[i][j]=="S":
                if  ((i<rows-1) and area[i+1][j]=="W"):
                    print("No", end="")
                    return
                elif  ((i<rows-1) and area[i+1][j]=="."):
                    area[i+1][j]="D"
                if (i>0 and area[i-1][j]=="W"):
                    print("No", end="")
                    return
                elif ((i>0) and area[i-1][j]=="."):
                    area[i-1][j]="D"
                if ((j<columnns-1) and area[i][j+1]=="W"):
                    print("No", end="")
                    return
                elif ((j<columnns-1) and area[i][j+1]=="."):
                    area[i][j+1]="D"
                if ((j>0) and area[i][j-1]=="W"):
                    print("No", end="")
                    return
                elif ((j>0) and area[i][j-1]=="."):
                    area[i][j-1]="D"
    print("Yes")       
    for i in range(rows):
        for j in range(columnns):
            print(area[i][j], end="")
        if i< rows-1:
            print()
            
solution()
 	 			      		 	 	    	 	 	 	 	

C++ Code:

// Problem: A. Protect Sheep
// Contest: Codeforces - Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)
// URL: https://codeforces.com/problemset/problem/948/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms

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

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int r,c;
    
    cin>>r>>c;
    string a[r];
    for (long i = 0; i < r; i++){
			cin>>a[i];
			// cerr<<a[i]<<"\n";
        }		
    for (long i = 0; i < r; i++){
    	string s=a[i];
    	for (long j=0;j<c;j++){
    		if (s[j]!='S'){
    			continue;
    		}
    		if (j>0){
    			
				if (s[j-1]=='W'){
					cout<<"No";
					return(0);
				}
				else if (s[j-1]=='.'){
					a[i][j-1]='D';
				}
			}
			if (j<c-1){
				if (s[j+1]=='W'){
					// cerr<<"me";
					cout<<"No"<<endl;
					return(0);
				}
				else if (s[j+1]=='.'){
					a[i][j+1]='D';
				}

			}
			if (i>0){
				if (a[i-1][j]=='W'){
					cout<<"No";
					return 0;
				}
				else if (a[i-1][j]=='.'){
					a[i-1][j]='D';
				}
			}
			if (i<r-1){
				if (a[i+1][j]=='W'){
					cout<<"No";
					return 0;
				}
				else if (a[i+1][j]=='.'){
					a[i+1][j]='D';
				}

			}

        }
        // cerr<<2<<"\n";			

        }
        cout<<"Yes";
        for (long i = 0; i < r; i++){
        	// cerr<<a[i]<<"\n";
        	cout<<"\n"<<a[i];
        	

        }			

        }							
    


Comments

Submit
0 Comments
More Questions

32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement