Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
Изминување на лавиринт Problem 2 (1 / 1) Нека е даден совршен лавиринт (во форма како на аудиториските вежби - како влез од карактери). Ваша задача е да изгенерирате граф со листа на соседство (ненасочен и нетежински) од дадениот влез и да ја испечатите патеката од почетното (темето означено со S) до крајното теме (темето означено со Е). Патеката ја печатите на следниот начин: ги печатите координатите на јазлите кои ги изминувате (т.е ги печатите позициите од влезот) Име на класа: Lavirint ==================================================================================================================================== import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Hashtable; import java.util.Stack; class Lavirint { static class Graph { int num_nodes; // broj na jazli int adjMat[][]; // matrica na sosednost public Graph(int num_nodes) { this.num_nodes = num_nodes; adjMat = new int[num_nodes][num_nodes]; for(int i=0;i<this.num_nodes;i++) for(int j=0;j<this.num_nodes;j++) adjMat[i][j]=0; } public Graph(int num_nodes, int[][] adjMat) { this.num_nodes = num_nodes; this.adjMat = adjMat; } int adjacent(int x,int y) { // proveruva dali ima vrska od jazelot so indeks x do jazelot so indeks y return (adjMat[x][y]!=0)?1:0; } void addEdge(int x,int y) { // dodava vrska megu jazlite so indeksi x i y adjMat[x][y]=1; adjMat[y][x]=1; } void deleteEdge(int x,int y) { // ja brise vrskata megu jazlite so indeksi x i y adjMat[x][y]=0; adjMat[y][x]=0; } public int getNum_nodes() { return num_nodes; } public void setNum_nodes(int num_nodes) { this.num_nodes = num_nodes; } @Override public String toString() { String ret=" "; for(int i=0;i<num_nodes;i++) ret+=i+" "; ret+="\n"; for(int i=0;i<num_nodes;i++){ ret+=i+" "; for(int j=0;j<num_nodes;j++) ret+=adjMat[i][j]+" "; ret+="\n"; } return ret; } } static class Maze { //Kje se koristi nenasocen graf implementiran so martica na sosednost //za pretstavuvanje na lavirintot //Teminjata vo grafot se samo indeksi //Iminjata na teminjata se preveduvaat vo broevi preku hash tabela //T.e. na pr. 1,1 kje se prevede vo 1 (1,1 kje se zacuva kako string) Graph g; int start_node; //indeks temeto koe e vlez int end_node; //indeks na temeto koe e izlez Hashtable<String,Integer> h; Hashtable<Integer,String> rehash; public Maze() { h = new Hashtable<String,Integer>(); rehash = new Hashtable<Integer,String>(); } void generateGraph(int rows, int columns, String[] in){ int num_nodes = 0; String key; for(int i=1; i<rows; i++){ for(int j=1; j<columns; j++){ if(in[i].charAt(j)!='#'){ key = i+","+j; rehash.put(num_nodes+1,key); h.put(key, num_nodes); if(in[i].charAt(j)=='S') start_node = num_nodes; if(in[i].charAt(j)=='E') end_node = num_nodes; num_nodes++; } } } g = new Graph(num_nodes); int x; int y; //generiranje na matrica na sosednost for(int i=1; i<rows; i++){ for(int j=1; j<columns; j++){ if(in[i].charAt(j)!='#'){ //dali ima teme pred nego if(in[i].charAt(j-1)!='#'){ x = h.get(i+","+j); y = h.get(i+","+(j-1)); g.addEdge(x, y); } //dali ima teme posle nego if(in[i].charAt(j+1)!='#'){ x = h.get(i+","+j); y = h.get(i+","+(j+1)); g.addEdge(x, y); } //dali ima teme nad nego if(in[i-1].charAt(j)!='#'){ x = h.get(i+","+j); y = h.get((i-1)+","+j); g.addEdge(x, y); } //dali ima teme pod nego if(in[i+1].charAt(j)!='#'){ x = h.get(i+","+j); y = h.get((i+1)+","+j); g.addEdge(x, y); } } } } } void findPath(){ boolean visited[] = new boolean[g.getNum_nodes()]; for (int i = 0; i < g.getNum_nodes(); i++) visited[i] = false; visited[start_node] = true; Stack<Integer> s = new Stack<Integer>(); s.push(start_node); int pom; while (!s.isEmpty() && s.peek()!=end_node) { pom = s.peek(); int pom1 = pom; for (int i = 0; i < g.getNum_nodes(); i++) { if(g.adjacent(pom,i)==1){ pom1 = i; if(!visited[i]) break; } } if(!visited[pom1]){ visited[pom1] = true; //System.out.println(tmp.getIndex() + ": " + tmp.getInfo()); s.push(pom1); } else s.pop(); } if(s.isEmpty()) System.out.println("Nema reshenie"); else{ Stack<Integer> os = new Stack<Integer>(); while(!s.isEmpty()) os.push(s.pop()); while(!os.isEmpty()){ int key = os.pop(); System.out.println(rehash.get(key+1)); } } } public static void main(String args[]) throws IOException { Maze m = new Maze(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = in.readLine(); String pom[] = line.split(","); int rows = Integer.parseInt(pom[0]); int columns = Integer.parseInt(pom[1]); String input[] = new String[rows]; for(int i = 0; i < rows; i++){ input[i] = in.readLine(); } m.generateGraph(rows, columns, input); System.out.println("Pateka:"); m.findPath(); /* String[] in = new String[rows]; Maze m = new Maze(); int rows = 6; int columns = 6; in[0] = "######"; in[1] = "# # ##"; in[2] = "# # S#"; in[3] = "# # ##"; in[4] = "# E #"; in[5] = "######"; m.generateGraph(rows, columns, in); System.out.println("Pateka:"); m.findPath(); */ } } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
5 hours ago | 88.05 KB
Untitled
7 hours ago | 58.79 KB
z66is_archive.zip.txt
13 hours ago | 1.05 MB
Untitled
16 hours ago | 71.19 KB
Untitled
16 hours ago | 64.82 KB
Untitled
16 hours ago | 88.55 KB
Untitled
16 hours ago | 65.48 KB
Untitled
17 hours ago | 95.14 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!