컴퓨터공부/알고리즘 16

알고리즘 문제 풀때, 파일 입출력으로 입출력하고 싶을 때

각종 TC를 직접 손으로 집어 넣는 방법은... 시간이 많이 뺏긴다.그래서 좀 찾아봤는데.. 분명 뭔가 있었던 것 같은데 못 찾았다.. 그냥 구글링해서 대충 만들었는데.. 동작은 한다... 일단 이렇게 가야지.. + // ConsoleApplication1.cpp : Defines the entry point for the console application.// #include "stdafx.h"#include #include // std::ifstream using namespace std; int main(){std::ifstream cin("input.txt", ios::in);std::ofstream cout("output.txt", ios::out); int a; cin >> a; cout

계수정렬(counting sort)

// countingSort.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.// #include "stdafx.h"#include using namespace std; #define MAX 20 int array[] = { 5, 7, 3, 2, 7, 9, 1, 3, 8, 2 }; int sortret[MAX] = { 0, }; int countsort(int sortret[]){int count[MAX] = { 0, };int arraysize = sizeof(array) / sizeof(array[0]); for (int i = 0; i < arraysize; i++){count[array[i]] += 1 ;} for (int i = 1; i < arraysize; i++){count[i] ..

미로찾기(백트래킹, backtracking) - 지나간 모든 경로만 표시

#include int map[9][9] = {{2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 1, 1, 1, 0, 0, 0, 2}, {2, 0, 1, 0, 1, 0, 0, 0, 2}, {2, 0, 1, 0, 1, 0, 0, 0, 2}, {2, 0, 1, 1, 1, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2, 2, 2}}; int visit(int x, int y); void print(); int success; int start_x, start_y; int end_x, end_y; int sp, ri[100], r..

미로찾기(백트래킹, backtracking) - 모든 경로 출력

#include int map[9][9] = {{2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 2, 0, 2, 2, 0, 2}, {2, 0, 2, 0, 0, 2, 0, 0, 2}, {2, 0, 2, 0, 2, 0, 2, 0, 2}, {2, 0, 0, 0, 0, 0, 2, 0, 2}, {2, 2, 0, 2, 2, 0, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2, 2, 2}}; int visit(int x, int y); void print_path(); int success; int start_x, start_y; int end_x, end_y; int sp, ri[10..

미로찾기(백트래킹, backtracking) - 지나간 경로 출력

#include int map[7][7] = {{2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 0, 2, 0, 2}, {2, 0, 0, 2, 0, 2, 2}, {2, 2, 0, 2, 0, 2, 2}, {2, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2}}; int visit(int x, int y); void print_path(); int success; int start_x, start_y; int end_x, end_y; int sp, ri[100], rj[100]; void main() { sp = 0; start_x = 1; start_y = 1; // 시작지점 end_x = 5; end_y = 5; // 최종도착지점..

대각선 순으로 배열 값 입력

코드들이 맘에 들지 않는다.. + #include int map[9][9]; FILE *in = fopen("input.txt", "r"); FILE *out = fopen("output.txt", "w"); void print(int num) { int i, j; for(i =0 ; i < num; i++) { for(j =0 ; j < num; j++) fprintf(out,"%d ", map[i][j]); fprintf(out, "\n"); } } void assign( int num) { int i, j; int increase; // 오른쪽으로 갈때마다 증가되는 값 int divide; // 오른쪽 대각선으로 가르는 것 확인 int plus_num; for(i =0 ; i < num; i++) ..

미로찾기(백트래킹, backtracking)

#include int map[7][7] = {{2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 0, 2, 0, 2}, {2, 0, 0, 2, 0, 2, 2}, {2, 2, 0, 2, 0, 2, 2}, {2, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2}}; int visit(int x, int y); int success; int start_x, start_y; int end_x, end_y; void main() { start_x = 1; start_y = 1; // 시작지점 end_x = 5; end_y = 5; // 최종도착지점 success = 0; printf("미로 찾기\n"); if(!visit(1,1)) prin..