สมมติว่าเราได้รับตารางที่มี 2 แถวและ n คอลัมน์ หุ่นยนต์อยู่ที่ตำแหน่ง (0, 0) ในตารางและต้องการเยี่ยมชม (1, n - 1) โดยไปที่เซลล์ที่อยู่ติดกันและมุมไปยังตำแหน่งปัจจุบัน เราได้รับตารางในอาร์เรย์ของสตริง เซลล์จะถูกบล็อกหากมีการทำเครื่องหมาย '#' และสามารถเข้าถึงได้หากมีการทำเครื่องหมาย '.' เราต้องค้นหาว่าหุ่นยนต์สามารถเยี่ยมชมเซลล์ (1, n - 1) จากเซลล์ (0, 0) ได้หรือไม่
ดังนั้น หากอินพุตเป็น n =4, grid ={".##.", "...."} ผลลัพธ์ก็จะเป็นไปได้
ขั้นตอน
เพื่อแก้ปัญหานี้ เราจะทำตามขั้นตอนเหล่านี้ -
flag := 1
for initialize i := 0, when i < n, update (increase i by 1), do:
if grid[0, i] is same as '#' and grid[1, i] is same as '#',
then:
flag := 0
if flag is same as 0, then:
print("Not Possible.")
Otherwise
print("Possible.") ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, string grid[]) {
int flag = 1;
for(int i = 0; i < n; i++){
if(grid[0].at(i) == '#' && grid[1].at(i) == '#'){
flag = 0;
}
}
if (flag == 0)
cout<<"Not Possible.";
else
cout<<"Possible.";
}
int main() {
int n = 4;
string grid[] = {".##.", "...."};
solve(n, grid);
return 0;
} อินพุต
4, {".##.", "...."} ผลลัพธ์
Possible.