15723번: n단 논법

2024. 5. 5. 23:01· Koala - 14기/코딩테스트 준비 스터디

https://www.acmicpc.net/problem/15723

  • 코드
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdio.h>
#include <queue>
#include <vector>
#include <unordered_map>
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <deque>
#include <bitset>

using namespace std;

vector <int> graph[27];


int bfs(int start,int find)
{
	bool visited[27] = { false, };
	queue<char> q;
	q.push(start);
	visited[start] = true;

	int ans = 0;

	// 큐가 빌 때까지 반복
	while (!q.empty()) {
		// 큐에서 하나의 원소를 뽑아 출력
		int x = q.front();
		if (find == x) {
			ans = 1;
		}
	
		q.pop();
		// 해당 원소와 연결된, 아직 방문하지 않은 원소들을 큐에 삽입
		for (int i = 0; i < graph[x].size(); i++) {
			int y = graph[x][i];
	
			if (!visited[y]) {
				q.push(y);
				visited[y] = true;
				
			}
		}
	}
	return ans;
}
int main()
{
	int n;
	cin >> n;
	cin.ignore();
	for (int i = 0; i < n; i++) {
		string s1;
		getline(cin, s1);
		graph[s1[0] - 97].push_back(s1[5] - 97);
	}
	int m;
	cin >> m;
	cin.ignore();
	for (int i = 0; i < m; i++) {
		string s2;
		getline(cin, s2);
		int check=bfs(s2[0] - 97, s2[5]-97);
		if (check == 0) {
			cout << "F" << endl;
		}
		else cout << "T" << endl;
	}
}
  • 알고리즘 분류: 너비우선 탐색
  • 문제 해설

노드를 연결해주고 너비우선탐색을 이용하여 해당 명제가 참인지 알기 위해 "a이면 b이다"에서 a를 start노드로 탐색해나가면 되었다. 탐색하면서 b가 있으면 T를 출력, 찾지 못하면 F를 출력한다.

저작자표시 (새창열림)

'Koala - 14기 > 코딩테스트 준비 스터디' 카테고리의 다른 글

[PG/Java] Programmers 여행경로  (0) 2024.05.05
[백준 2606번 바이러스/ 파이썬]  (0) 2024.05.05
[백준/python] 2644 촌수계산  (0) 2024.05.05
[PG/Go] 단어 변환  (0) 2024.05.05
[Codeforces Round 943 (Div. 3)] Permutation Game  (0) 2024.05.03
'Koala - 14기/코딩테스트 준비 스터디' 카테고리의 다른 글
  • [PG/Java] Programmers 여행경로
  • [백준 2606번 바이러스/ 파이썬]
  • [백준/python] 2644 촌수계산
  • [PG/Go] 단어 변환
KauKoala
KauKoala
항공대 알고리즘 동아리 Koala 🥰
KauKoala
Koala
KauKoala
전체
오늘
어제
  • 분류 전체보기 (1889) N
    • 공지 게시판 (10)
    • 정보 게시판 (8)
    • Codeforce (15)
    • acm-icpc (6)
    • Koala - 1기 (16)
    • Koala - 2기 (111)
      • Programming Contest (1)
      • A반 (20)
      • B반 (39)
      • C반 (22)
      • 기초 강의 (18)
    • Koala - 3기 (10)
      • 기초 스터디 (7)
    • Koala - 4기 (67)
    • Koala - 5기 (144)
      • 기초 알고리즘 스터디 (75)
      • 코딩테스트 준비 스터디 (68)
    • Koala - 6기 (102)
      • 기초 알고리즘 스터디 (75)
      • 코딩테스트 준비 스터디 (25)
      • 모의 테스트 스터디 (1)
    • Koala - 7기 (167)
      • 기초 알고리즘 스터디 (97)
      • 코딩테스트 준비 스터디 (68)
      • 모의 테스트 스터디 (1)
    • Koala - 8기 (44)
      • 기초 알고리즘 스터디 (32)
      • 코딩테스트 준비 스터디 (10)
      • 코드포스 버츄얼 스터디 (0)
      • 프로그래머스 LV2 스터디 (0)
    • Koala - 9기 (205)
      • 기초 알고리즘 스터디 (138)
      • 코딩테스트 준비 스터디 (64)
      • 모의테스트 준비 스터디 (1)
    • Koala - 10기 (117)
      • 기초 알고리즘 스터디 (30)
      • 코딩테스트 준비 스터디 (86)
      • 모의테스트 준비 스터디 (1)
    • Koala - 11기 (151)
      • 기초 알고리즘 스터디 (46)
      • 코딩테스트 준비 스터디 (104)
      • 모의테스트 준비 스터디 (1)
    • Koala - 12기 (86)
      • 기초 알고리즘 스터디 (31)
      • 코딩테스트 준비 스터디 (55)
    • Koala - 13기 (119)
      • 기초 알고리즘 스터디 (52)
      • 코딩테스트 준비 스터디 (67)
    • Koala - 14기 (116)
      • 기초 알고리즘 스터디 (39)
      • 코딩테스트 준비 스터디 (77)
    • Koala - 15기 (138)
      • 기초 알고리즘 스터디 (73)
      • 코딩테스트 준비 스터디 (65)
    • Koala - 16기 (47)
      • 코딩테스트 기초 스터디 (16)
      • 코딩테스트 심화 스터디 (31)
    • Koala - 17기 (62)
      • 코딩테스트 기초 스터디 (15)
      • 코딩테스트 심화 스터디 (47)
    • Koala - 18기 (31)
      • 코딩테스트 기초 스터디 (11)
      • 코딩테스트 심화 스터디 (20)
    • Koala - 19기 (43) N
      • 코딩테스트 기초 스터디 (7)
      • 코딩테스트 심화 스터디 (36) N
    • Koala - 20기 (0)
      • 코딩테스트 기초 스터디 (0)
      • 코딩테스트 심화 스터디 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 🐨항공대 알고리즘 학회 Koala 3기 모집
  • 🐨항공대 알고리즘 학회 Koala 2기 모집
  • 소모임 소개

인기 글

태그

  • 파이썬
  • BOJ
  • BFS
  • dp
  • 백트래킹
  • 백준
  • dfs
  • C++

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.3.0
KauKoala
15723번: n단 논법
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.